SonarQube Java
  1. SonarQube Java
  2. SONARJAVA-238

Rule: Switch/case blocks should not contain too many lines

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.4
    • Component/s: Rules
    • Labels:
      None
    • Number of attachments :
      0

      Activity

      Hide
      Dinesh Bolkensteyn added a comment -

      Done

      Show
      Dinesh Bolkensteyn added a comment - Done
      Hide
      Freddy Mallet added a comment -

      When analyzing the following piece of code :

      int a = 4;
      switch(a){
        case 2:
      
        case 0:
          break;
      }
      {c
      

      The following exception is thrown :

      Caused by: java.lang.NullPointerException
      	at org.sonar.java.checks.SwitchCaseTooBigCheck.getNumberOfLines(SwitchCaseTooBigCheck.java:58)
      	at org.sonar.java.checks.SwitchCaseTooBigCheck.visitNode(SwitchCaseTooBigCheck.java:49)
      	at com.sonar.sslr.impl.ast.AstWalker.visitNode(AstWalker.java:114)
      	at com.sonar.sslr.impl.ast.AstWalker.visit(AstWalker.java:85)
      	at com.sonar.sslr.impl.ast.AstWalker.visitChildren(AstWalker.java:99)
      	at com.sonar.sslr.impl.ast.AstWalker.visit(AstWalker.java:87)
      	at com.sonar.sslr.impl.ast.AstWalker.visitChildren(AstWalker.java:99)
      	at com.sonar.sslr.impl.ast.AstWalker.visit(AstWalker.java:87)
      

      Moreover we don't get any issue in the following case whereas one is expected :

      int a = 4;
      switch(a){
        case 2:
          //comment 1
          //comment 2
          //comment 3
          //comment 4
          //comment 5
          //comment 6
          //comment 7
          //comment 8
          //comment 9
          break;
        case 0:
          break;
      }
      
      Show
      Freddy Mallet added a comment - When analyzing the following piece of code : int a = 4; switch (a){ case 2: case 0: break ; } {c The following exception is thrown : Caused by: java.lang.NullPointerException at org.sonar.java.checks.SwitchCaseTooBigCheck.getNumberOfLines(SwitchCaseTooBigCheck.java:58) at org.sonar.java.checks.SwitchCaseTooBigCheck.visitNode(SwitchCaseTooBigCheck.java:49) at com.sonar.sslr.impl.ast.AstWalker.visitNode(AstWalker.java:114) at com.sonar.sslr.impl.ast.AstWalker.visit(AstWalker.java:85) at com.sonar.sslr.impl.ast.AstWalker.visitChildren(AstWalker.java:99) at com.sonar.sslr.impl.ast.AstWalker.visit(AstWalker.java:87) at com.sonar.sslr.impl.ast.AstWalker.visitChildren(AstWalker.java:99) at com.sonar.sslr.impl.ast.AstWalker.visit(AstWalker.java:87) Moreover we don't get any issue in the following case whereas one is expected : int a = 4; switch (a){ case 2: //comment 1 //comment 2 //comment 3 //comment 4 //comment 5 //comment 6 //comment 7 //comment 8 //comment 9 break ; case 0: break ; }
      Hide
      Dinesh Bolkensteyn added a comment -

      We now count the number of lines between cases, so:

      case 0: foo(); bar(); // Has just 2 lines
        baz();
      case 1:
        /* .. */
      

      and

      case 0: // Has 0 lines
        foo(); case 1: bar();
      

      These are corner cases anyway

      Show
      Dinesh Bolkensteyn added a comment - We now count the number of lines between cases, so: case 0: foo(); bar(); // Has just 2 lines baz(); case 1: /* .. */ and case 0: // Has 0 lines foo(); case 1: bar(); These are corner cases anyway
      Hide
      Dinesh Bolkensteyn added a comment -

      Let's remove a bit of magic on how lines are computed

      Show
      Dinesh Bolkensteyn added a comment - Let's remove a bit of magic on how lines are computed
      Hide
      Dinesh Bolkensteyn added a comment -

      Done

      Show
      Dinesh Bolkensteyn added a comment - Done
      Hide
      Freddy Mallet added a comment -

      Manually tested !

      Show
      Freddy Mallet added a comment - Manually tested !

        People

        • Assignee:
          Dinesh Bolkensteyn
          Reporter:
          Freddy Mallet
        • Votes:
          0 Vote for this issue
          Watchers:
          2 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: