Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3750

S1199 should not report an issue for any Switch case containing a block

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.15
    • Component/s: Rules
    • Labels:

      Description

      Currently, S1199 reports an issue on any block in a case group. This approach is too naive, and result in issues with actions that do not make sense:

            case "d":
            { // Noncompliant {{Extract this nested code block into a method.}} --> Really?!
              doSomething();
              break;
            }
      

      In fact, this rule should not report an issue when the body of the case is a single block. If there is something to refactor, RSPEC-1151 will report it.

            case "d":
            { // Compliant
              doSomething();
              break;
            } case "e":
            { // Compliant
              int a = 123;
              doSomething(a);
              break;
            }
      

      This includes the new way to write switch in Java 14:

      int i = switch (mode) {
        case "a" -> {        // Compliant, currently a FP
          yield 1;
        }
        default -> {         // Compliant, currently a FP
          doSomething();
          yield 2;
        }
      };
      
      switch (mode) {
        case "a" -> {       // Compliant, currently a FP
          result = 1;
          break;
        }
        default -> {         // Compliant, currently a FP
          doSomethingElse();
          result = 2;
          break;
        }
      };
      

      Still, when the body contains multiples statements and one of them is a block, the issue makes sense:

            case "d":
            { // Noncompliant 
              doSomething();
              break;
            }
            doSomething();
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                quentin.jaquier Quentin Jaquier
                Reporter:
                quentin.jaquier Quentin Jaquier
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: