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

S128 should be altered to detect only cases when the switch "fall-through" feature is actually used

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.1
    • Component/s: Rules
    • Labels:
      None

      Description

      This ticket relates to http://jira.sonarsource.com/browse/RSPEC-128, which will need to be updated.

      Currently, the rule title is "Switch cases should end with an unconditional break statement".

      That rule is in fact too strong, as there is no real issue on code where the last case has no break, such as:

      switch (myVariable) {
        case 0:
          doSomething();
          break;
        default: // There is no break here, however there is also no "fall through", as it is the last swtich case.
          doSomethingElse();
      }
      

      So it would be better to rename the rule into something like "Non-empty switch cases should not fall-through the next one.".

      So, here is the newly wanted behavior:

      switch (myVariable) {
        case 0: // Compliant - this case is empty
        case 1: // Compliant - this case is non-empty but contains a break
          doSomething1();
          break;
        case 2: // Compliant
          if (/* ... */) {
            doeSomething2();
            break;
          } else {
            throw new IllegalStateException();
          }
        default: // Noncompliant
          doSomething3();
        case 3: // Noncompliant
          doSomething4();
        case 4: // Compliant
          doSomething5();
          break;
        case 5: // Compliant
        case 6: // Compliant
        case 7: // Noncompliant - missing a break/return/throw/continue
          doSomething6();
        case 8:  // Compliant - this is the last case, which does not need to break
          doSomething7();
      }
      

        Attachments

          Activity

            People

            Assignee:
            benzonico OLD - Nicolas Peru
            Reporter:
            dinesh.bolkensteyn Dinesh Bolkensteyn (Inactive)
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Due:
              Created:
              Updated:
              Resolved: