View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule4842fallthrough;
2   
3   /** Some javadoc. */
4   public class InputFallThrough {
5     void method(int i, int j, boolean cond) {
6       while (true) {
7         switch (i) {
8           case 0: // no problem
9           case 1:
10            i++;
11            break;
12          case 2:
13            i++;
14          case 3: // violation 'Fall through from previous branch of the switch statement.'
15            i++;
16            break;
17          case 4:
18            return;
19          case 5:
20            throw new RuntimeException("");
21          case 6:
22            continue;
23          case 7:
24            { // violation 'should be on the previous line'
25              break;
26            }
27          case 8:
28            { // violation 'should be on the previous line'
29              return;
30            }
31          case 9:
32            { // violation 'should be on the previous line'
33              throw new RuntimeException("");
34            }
35          case 10:
36            { // violation 'should be on the previous line'
37              continue;
38            }
39          case 11:
40            { // violation 'should be on the previous line'
41              i++;
42            }
43          case 12: // violation 'Fall through from previous branch of the switch statement.'
44            if (false) {
45              break;
46            } else {
47              break;
48            }
49          case 13:
50            if (true) {
51              return;
52            }
53          case 14: // violation 'Fall through from previous branch of the switch statement.'
54            if (true) {
55              return;
56            } else {
57              // do nothing
58            }
59          case 15: // violation 'Fall through from previous branch of the switch statement.'
60            do {
61              System.identityHashCode("something");
62              return;
63            } while (true);
64          case 16:
65            for (int j1 = 0; j1 < 10; j1++) {
66              System.identityHashCode("something");
67              return;
68            }
69          case 17:
70            while (true) {
71              throw new RuntimeException("");
72            }
73          case 18:
74            while (cond) {
75              break;
76            }
77          case 19: // violation 'Fall through from previous branch of the switch statement.'
78            try {
79              i++;
80              break;
81            } catch (RuntimeException e) {
82              break;
83            } catch (Error e) {
84              return;
85            }
86          case 20:
87            try {
88              i++;
89              break;
90            } catch (RuntimeException e) {
91              // do nothing
92            } catch (Error e) {
93              return;
94            }
95          case 21: // violation 'Fall through from previous branch of the switch statement.'
96            try {
97              i++;
98            } catch (RuntimeException e) {
99              i--;
100           } finally {
101             break;
102           }
103         case 22:
104           try {
105             i++;
106             break;
107           } catch (RuntimeException e) {
108             i--;
109             break;
110           } finally {
111             i++;
112           }
113         case 23:
114           switch (j) {
115             case 1:
116               continue;
117             case 2:
118               return;
119             default:
120               return;
121           }
122         case 24:
123           switch (j) {
124             case 1:
125               continue;
126             case 2:
127               break;
128             default:
129               return;
130           }
131         default: // violation 'Fall through from previous branch of the switch statement.'
132           // this is the last label
133           i++;
134       }
135     }
136   }
137 
138   /* Like above, but all fall throughs with relief comment */
139   void methodFallThru(int i, int j, boolean cond) {
140     while (true) {
141       switch (i) {
142         case -1: // FALLTHRU
143 
144         case 0: // no problem
145         case 1:
146           i++;
147           break;
148         case 2:
149           i++;
150           // fallthru
151         case 3:
152           i++;
153           break;
154         case 4:
155           return;
156         case 5:
157           throw new RuntimeException("");
158         case 6:
159           continue;
160         case 7:
161           { // violation 'should be on the previous line'
162             break;
163           }
164         case 8:
165           { // violation 'should be on the previous line'
166             return;
167           }
168         case 9:
169           { // violation 'should be on the previous line'
170             throw new RuntimeException("");
171           }
172         case 10:
173           { // violation 'should be on the previous line'
174             continue;
175           }
176         case 11:
177           { // violation 'should be on the previous line'
178             i++;
179           }
180           // fallthru
181         case 12:
182           if (false) {
183             break;
184           } else {
185             break;
186           }
187         case 13:
188           if (true) {
189             return;
190           }
191         case 14: // violation 'Fall through from previous branch of the switch statement.'
192           if (true) {
193             return;
194           } else {
195             // do nothing
196           }
197           // fallthru
198         case 15:
199           do {
200             System.identityHashCode("something");
201             return;
202           } while (true);
203         case 16:
204           for (int j1 = 0; j1 < 10; j1++) {
205             System.identityHashCode("something");
206             return;
207           }
208         case 17:
209           while (cond) {
210             throw new RuntimeException("");
211           }
212         case 18:
213           while (cond) {
214             break;
215           }
216           // fallthru
217         case 19:
218           try {
219             i++;
220             break;
221           } catch (RuntimeException e) {
222             break;
223           } catch (Error e) {
224             return;
225           }
226         case 20:
227           try {
228             i++;
229             break;
230           } catch (RuntimeException e) {
231             return;
232           } catch (Error e) {
233             return;
234           }
235           // fallthru
236         case 21:
237           try {
238             i++;
239           } catch (RuntimeException e) {
240             i--;
241           } finally {
242             break;
243           }
244         case 22:
245           try {
246             i++;
247             break;
248           } catch (RuntimeException e) {
249             i--;
250             break;
251           } finally {
252             i++;
253           }
254           /* fallthru */
255         case 23:
256           switch (j) {
257             case 1:
258               continue;
259             case 2:
260               return;
261             default:
262               return;
263           }
264         case 24:
265           i++;
266           // violation below 'incorrect indentation level 10, expected level should be 8'
267           /* fallthru */ case 25:
268           i++;
269           break;
270 
271         case 26:
272           switch (j) {
273             case 1:
274               continue;
275             case 2:
276               break;
277             default:
278               return;
279           }
280           // fallthru
281         default:
282           // this is the last label
283           i++;
284           // fallthru
285       }
286     }
287   }
288 
289   /* Test relief comment. */
290   void methodFallThruCc(int i, int j, boolean cond) {
291     while (true) {
292       switch (i) { // violation 'switch without "default" clause.'
293         case 0:
294           i++; // fallthru
295 
296         case 1:
297           i++;
298           // fallthru
299         case 2:
300           { // violation 'should be on the previous line'
301             i++;
302           }
303           // fallthru
304         case 3:
305           i++;
306           // violation below 'incorrect indentation level 10, expected level should be 8'
307           /* fallthru */ case 4:
308           break;
309         case 5:
310           i++;
311           // fallthru
312       }
313     }
314   }
315 
316   /* Like above, but C-style comments. */
317   void methodFallThruC(int i, int j, boolean cond) {
318     while (true) {
319       switch (i) { // violation 'switch without "default" clause.'
320         case 0:
321           i++; /* fallthru */
322 
323         case 1:
324           i++;
325           /* fallthru */
326         case 2:
327           i++;
328           // violation below 'incorrect indentation level 10, expected level should be 8'
329           /* fallthru */ case 3:
330           break;
331         case 4:
332           i++;
333           /* fallthru */
334       }
335     }
336   }
337 
338   /* Like above, but C-style comments with no spaces. */
339   void methodFallThruC2(int i, int j, boolean cond) {
340     while (true) {
341       switch (i) { // violation 'switch without "default" clause.'
342         case 0:
343           i++; /*fallthru*/
344 
345         case 1:
346           i++;
347           /*fallthru*/
348         case 2:
349           i++;
350           // violation below 'incorrect indentation level'
351           /*fallthru*/ case 3:
352           break;
353         case 4:
354           i++;
355           /*fallthru*/
356       }
357     }
358   }
359 
360   /* C-style comments with other default fallthru-comment. */
361   void methodFallThruOtherWords(int i, int j, boolean cond) {
362     while (true) {
363       switch (i) { // violation 'switch without "default" clause.'
364         case 0:
365           i++; /* falls through */
366 
367         case 1:
368           i++;
369           /* falls through */
370         case 2:
371           i++;
372           // violation below 'incorrect indentation level'
373           /* falls through */ case 3:
374           break;
375         case 4:
376           i++;
377           /* falls through */
378       }
379     }
380   }
381 
382   /* C-style comments with custom fallthru-comment. */
383   void methodFallThruCustomWords(int i, int j, boolean cond) {
384     while (true) {
385       switch (i) { // violation 'switch without "default" clause.'
386         case 0:
387           i++; /* Continue with next case */
388 
389         case 1: // violation 'Fall through from previous branch of the switch statement.'
390           i++;
391           /* Continue with next case.  */
392         case 2: // violation 'Fall through from previous branch of the switch statement.'
393           i++;
394           /* Continue with next case. */ case 3:
395           // 2 violations above:
396           //   'Fall through from previous branch of the switch statement.'
397           //   'incorrect indentation level'
398           break;
399         case 4:
400           i++;
401           /* Continue with next case */
402       }
403     }
404   }
405 
406   void methodFallThruLastCaseGroup(int i, int j, boolean cond) {
407     while (true) {
408       switch (i) { // violation 'switch without "default" clause.'
409         case 0:
410           i++; // fallthru
411       }
412       switch (i) { // violation 'switch without "default" clause.'
413         case 0:
414           i++;
415           // fallthru
416       }
417       switch (i) { // violation 'switch without "default" clause.'
418         case 0:
419           i++;
420           /* fallthru */
421       }
422     }
423   }
424 
425   void method1472228(int i) {
426     switch (i) {
427       case 2:
428         // do nothing
429         break;
430       default:
431     }
432   }
433 
434   /* Test relief comment. */
435   void methodFallThruWithDash(int i, int j, boolean cond) {
436     while (true) {
437       switch (i) {
438         case 0:
439           i++; // fallthru
440         case 1:
441           i++; // fall thru
442         case 2:
443           i++; // fall-thru
444         case 3:
445           i++; // fallthrough
446         case 4:
447           i++; // fall through
448         case 5:
449           i++; // fall-through
450         case 6:
451           i++; // fallsthru
452         case 7:
453           i++; // falls thru
454         case 8:
455           i++; // falls-thru
456         case 9:
457           i++; // fallsthrough
458         case 10:
459           i++; // falls through
460         case 11:
461           i++; // falls-through
462         case 12:
463           i++; // fall--through
464         case 13: // violation 'Fall through from previous branch of the switch statement.'
465           i++; // fall+through
466         case 14: // violation 'Fall through from previous branch of the switch statement.'
467           i++; // falls_thru
468         case 15: // violation 'Fall through from previous branch of the switch statement.'
469           i++; // falls=through
470         case 16: // violation 'Fall through from previous branch of the switch statement.'
471           i++; // falls-throug
472         default: // violation 'Fall through from previous branch of the switch statement.'
473           throw new RuntimeException();
474       }
475     }
476   }
477 }