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