xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-fallthrough.cpp (revision 77f191e81ed475fbbc8eaa2e2dbe6a0a02f3b0c2)
1 // RUN: %check_clang_tidy %s bugprone-branch-clone %t -- -- -std=c++17
2 
3 void handle(int);
4 
testSwitchFallthroughAttribute(int value)5 void testSwitchFallthroughAttribute(int value) {
6   switch(value) {
7     case 1: [[fallthrough]];
8     case 2: [[fallthrough]];
9     case 3:
10       handle(value);
11       break;
12     default:
13       break;
14   }
15 }
16 
testSwitchFallthroughAttributeAndBraces(int value)17 void testSwitchFallthroughAttributeAndBraces(int value) {
18   switch(value) {
19     case 1: { [[fallthrough]]; }
20     case 2: { [[fallthrough]]; }
21     case 3: {
22       handle(value);
23       break;
24     }
25     default: {
26       break;
27     }
28   }
29 }
30 
testSwitchWithFallthroughAttributeAndCode(int value)31 void testSwitchWithFallthroughAttributeAndCode(int value) {
32   switch(value) {
33     case 1: value += 1; [[fallthrough]];
34     case 2: value += 1; [[fallthrough]];
35     case 3:
36       handle(value);
37       break;
38     default:
39       break;
40   }
41 }
42 
testSwitchWithFallthroughAndCode(int value)43 void testSwitchWithFallthroughAndCode(int value) {
44   switch(value) {
45     // CHECK-MESSAGES: :[[@LINE+1]]:5: warning: switch has 2 consecutive identical branches [bugprone-branch-clone]
46     case 1: value += 1;
47     case 2: value += 1;
48     // CHECK-MESSAGES: :[[@LINE-1]]:23: note: last of these clones ends here
49     case 3:
50       handle(value);
51       break;
52     default:
53       break;
54   }
55 }
56 
testSwitchFallthroughAttributeIntoDefault(int value)57 void testSwitchFallthroughAttributeIntoDefault(int value) {
58   switch(value) {
59     case 1: [[fallthrough]];
60     case 2: [[fallthrough]];
61     default:
62       handle(value);
63       break;
64   }
65 }
66