View Javadoc
1   /*
2   NeedBraces
3   allowSingleLineStatement = true
4   allowEmptyLoopBody = true
5   tokens = LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE, \
6            LITERAL_CASE, LITERAL_DEFAULT, LAMBDA
7   
8   
9   */
10  
11  package com.puppycrawl.tools.checkstyle.checks.blocks.needbraces;
12  
13  class InputNeedBracesTestItWithAllowsOn
14  {
15      /** @return helper func **/
16      boolean condition()
17      {
18          return false;
19      }
20  
21      /** Test do/while loops **/
22      void testDoWhile()
23      {
24          // Valid
25          do {
26              testDoWhile();
27          }
28          while (condition());
29  
30          // Invalid
31          do testDoWhile(); while (condition());
32      }
33  
34      /** Test while loops **/
35      void testWhile()
36      {
37          // Valid
38          while (condition()) {
39              testWhile();
40          }
41  
42          // Invalid
43          while(condition());
44          while (condition()) // violation
45              testWhile();
46          while (condition())
47              if (condition()) // violation
48                  testWhile();
49      }
50  
51      /** Test for loops **/
52      void testFor()
53      {
54          // Valid
55          for (int i = 1; i < 5; i++) {
56              testFor();
57          }
58  
59          // Invalid
60          for(int i = 1;i < 5;i++);
61          for (int i = 1; i < 5; i++) // violation
62              testFor();
63          for (int i = 1; i < 5; // violation
64               i++)
65              if (i > 2) // violation
66                  testFor();
67      }
68  
69      /** Test if constructs **/
70      public void testIf()
71      {
72          // Valid
73          if (condition()) {
74              testIf();
75          }
76          else if (condition()) {
77              testIf();
78          }
79          else {
80              testIf();
81          }
82  
83          // Invalid
84          if (condition());
85          if (condition()) // violation
86              testIf();
87          if (condition()) // violation
88              testIf();
89          else // violation
90              testIf();
91          if (condition()) // violation
92              testIf();
93          else {
94              testIf();
95          }
96          if (condition()) {
97              testIf();
98          }
99          else // violation
100             testIf();
101         if (condition()) // violation
102             if (condition()) // violation
103                 testIf();
104 
105         if (condition()) // violation
106             while (condition()) testWhile(); // violation
107         if (condition()) // violation
108             do testDoWhile(); while (condition()); // violation
109         if (condition()) // violation
110             for (int i = 0; i < 1; i++) testFor(); // violation
111         int a = 0;
112         switch (a) {default: {}}
113     }
114 
115     void whitespaceAfterSemi()
116     {
117         //reject
118         int i = 1;int j = 2;
119 
120         //accept
121         for (;;) {
122         }
123     }
124 
125     /** Empty constructor block. **/
126     public InputNeedBracesTestItWithAllowsOn() {}
127 
128     /** Empty method block. **/
129     public void emptyImplementation() {}
130 
131     public void method() {
132         if (true
133             || true || true) return;
134     }
135 }