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            {
25              break;
26            }
27          case 8:
28            {
29              return;
30            }
31          case 9:
32            {
33              throw new RuntimeException("");
34            }
35          case 10:
36            {
37              continue;
38            }
39          case 11:
40            {
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           {
162             break;
163           }
164         case 8:
165           {
166             return;
167           }
168         case 9:
169           {
170             throw new RuntimeException("");
171           }
172         case 10:
173           {
174             continue;
175           }
176         case 11:
177           {
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         /* fallthru */ case 25:
267           i++;
268           break;
269 
270         case 26:
271           switch (j) {
272             case 1:
273               continue;
274             case 2:
275               break;
276             default:
277               return;
278           }
279         // fallthru
280         default:
281           // this is the last label
282           i++;
283           // fallthru
284       }
285     }
286   }
287 
288   /* Test relief comment. */
289   void methodFallThruCc(int i, int j, boolean cond) {
290     while (true) {
291       switch (i) { // violation 'switch without "default" clause.'
292         case 0:
293           i++; // fallthru
294 
295         case 1:
296           i++;
297         // fallthru
298         case 2:
299           {
300             i++;
301           }
302         // fallthru
303         case 3:
304           i++;
305         /* fallthru */ case 4:
306           break;
307         case 5:
308           i++;
309           // fallthru
310       }
311     }
312   }
313 
314   /* Like above, but C-style comments. */
315   void methodFallThruC(int i, int j, boolean cond) {
316     while (true) {
317       switch (i) { // violation 'switch without "default" clause.'
318         case 0:
319           i++; /* fallthru */
320 
321         case 1:
322           i++;
323         /* fallthru */
324         case 2:
325           i++;
326         /* fallthru */ case 3:
327           break;
328         case 4:
329           i++;
330           /* fallthru */
331       }
332     }
333   }
334 
335   /* Like above, but C-style comments with no spaces. */
336   void methodFallThruC2(int i, int j, boolean cond) {
337     while (true) {
338       switch (i) { // violation 'switch without "default" clause.'
339         case 0:
340           i++; /*fallthru*/
341 
342         case 1:
343           i++;
344         /*fallthru*/
345         case 2:
346           i++;
347         /*fallthru*/ case 3:
348           break;
349         case 4:
350           i++;
351           /*fallthru*/
352       }
353     }
354   }
355 
356   /* C-style comments with other default fallthru-comment. */
357   void methodFallThruOtherWords(int i, int j, boolean cond) {
358     while (true) {
359       switch (i) { // violation 'switch without "default" clause.'
360         case 0:
361           i++; /* falls through */
362 
363         case 1:
364           i++;
365         /* falls through */
366         case 2:
367           i++;
368         /* falls through */ case 3:
369           break;
370         case 4:
371           i++;
372           /* falls through */
373       }
374     }
375   }
376 
377   /* C-style comments with custom fallthru-comment. */
378   void methodFallThruCustomWords(int i, int j, boolean cond) {
379     while (true) {
380       switch (i) { // violation 'switch without "default" clause.'
381         case 0:
382           i++; /* Continue with next case */
383 
384         case 1: // violation 'Fall through from previous branch of the switch statement.'
385           i++;
386         /* Continue with next case.  */
387         case 2: // violation 'Fall through from previous branch of the switch statement.'
388           i++;
389         /* Continue with next case. */ case 3:
390           // violation above 'Fall through from previous branch of the switch statement.'
391           break;
392         case 4:
393           i++;
394           /* Continue with next case */
395       }
396     }
397   }
398 
399   void methodFallThruLastCaseGroup(int i, int j, boolean cond) {
400     while (true) {
401       switch (i) { // violation 'switch without "default" clause.'
402         case 0:
403           i++; // fallthru
404       }
405       switch (i) { // violation 'switch without "default" clause.'
406         case 0:
407           i++;
408           // fallthru
409       }
410       switch (i) { // violation 'switch without "default" clause.'
411         case 0:
412           i++;
413           /* fallthru */
414       }
415     }
416   }
417 
418   void method1472228(int i) {
419     switch (i) {
420       case 2:
421         // do nothing
422         break;
423       default:
424     }
425   }
426 
427   /* Test relief comment. */
428   void methodFallThruWithDash(int i, int j, boolean cond) {
429     while (true) {
430       switch (i) {
431         case 0:
432           i++; // fallthru
433         case 1:
434           i++; // fall thru
435         case 2:
436           i++; // fall-thru
437         case 3:
438           i++; // fallthrough
439         case 4:
440           i++; // fall through
441         case 5:
442           i++; // fall-through
443         case 6:
444           i++; // fallsthru
445         case 7:
446           i++; // falls thru
447         case 8:
448           i++; // falls-thru
449         case 9:
450           i++; // fallsthrough
451         case 10:
452           i++; // falls through
453         case 11:
454           i++; // falls-through
455         case 12:
456           i++; // fall--through
457         case 13: // violation 'Fall through from previous branch of the switch statement.'
458           i++; // fall+through
459         case 14: // violation 'Fall through from previous branch of the switch statement.'
460           i++; // falls_thru
461         case 15: // violation 'Fall through from previous branch of the switch statement.'
462           i++; // falls=through
463         case 16: // violation 'Fall through from previous branch of the switch statement.'
464           i++; // falls-throug
465         default: // violation 'Fall through from previous branch of the switch statement.'
466           throw new RuntimeException();
467       }
468     }
469   }
470 }