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