View Javadoc
1   /*
2   AvoidNestedBlocks
3   allowInSwitchCase = true
4   
5   
6   */
7   
8   package com.puppycrawl.tools.checkstyle.checks.blocks.avoidnestedblocks;
9   
10  class InputAvoidNestedBlocksAllowInSwitchCase
11  {
12      static
13      {
14      }
15  
16      public void method()
17      {
18          int x = 0;
19  
20          // if (condition that is not important anymore)
21          { // violation 'Avoid nested blocks'
22              int z = 1;
23              int y = z;
24          }
25  
26          if (x == 1)
27          {
28              x = 2;
29          }
30  
31          // case statements are a bit complicated,
32          // they do not have its own variable scope by default.
33          // Hence, it may be ok in some development teams to allow
34          // nested blocks if they are the complete case body.
35          switch (x)
36          {
37              case 0:
38  
39                  x = 3;
40                  break;
41              case 1:
42                  // Not ok, SLIST is not complete case body
43                  { // violation 'Avoid nested blocks'
44                      x = 1;
45                  }
46                  break;
47              case 2:
48                  // ok if allowInSwitchCase is true, SLIST is complete case body
49                  {
50                      x = 1;
51                      break;
52                  }
53              case 3: // test fallthrough
54              default:
55                  // Not ok, SLIST is not complete case body
56                  System.identityHashCode("Hello");
57                  { // violation 'Avoid nested blocks'
58                      x = 2;
59                  }
60          }
61      }
62  }