1// RUN: %check_clang_tidy %s bugprone-suspicious-semicolon %t
2
3int x = 5;
4
5void nop();
6
7void correct1()
8{
9 if(x < 5) nop();
10}
11
12void correct2()
13{
14 if(x == 5)
15 nop();
16}
17
18void correct3()
19{
20 if(x > 5)
21 {
22 nop();
23 }
24}
25
26void fail1()
27{
28 if(x > 5); nop();
29 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: potentially unintended semicolon [bugprone-suspicious-semicolon]
30 // CHECK-FIXES: if(x > 5) nop();
31}
32
33void fail2()
34{
35 if(x == 5);
36 nop();
37 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: potentially unintended semicolon [bugprone-suspicious-semicolon]
38 // CHECK-FIXES: if(x == 5){{$}}
39}
40
41void fail3()
42{
43 if(x < 5);
44 {
45 nop();
46 }
47 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: potentially unintended semicolon
48 // CHECK-FIXES: if(x < 5){{$}}
49}
50
51void correct4()
52{
53 while(x % 5 == 1);
54 nop();
55}
56
57void correct5()
58{
59 for(int i = 0; i < x; ++i)
60 ;
61}
62
63void fail4()
64{
65 for(int i = 0; i < x; ++i);
66 nop();
67 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: potentially unintended semicolon
68 // CHECK-FIXES: for(int i = 0; i < x; ++i){{$}}
69}
70
71void fail5()
72{
73 if(x % 5 == 1);
74 nop();
75 // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: potentially unintended semicolon
76 // CHECK-FIXES: if(x % 5 == 1){{$}}
77}
78
79void fail6() {
80 int a = 0;
81 if (a != 0) {
82 } else if (a != 1);
83 a = 2;
84 // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: potentially unintended semicolon
85 // CHECK-FIXES: } else if (a != 1){{$}}
86}
87
88void fail7() {
89 if (true)
90 ;
91 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: potentially unintended semicolon
92}
93
94void correct6()
95{
96 do; while(false);
97}
98
99int correct7()
100{
101 int t_num = 0;
102 char c = 'b';
103 char *s = "a";
104 if (s == "(" || s != "'" || c == '"') {
105 t_num += 3;
106 return (c == ')' && c == '\'');
107 }
108
109 return 0;
110}
111
112void correct8() {
113 if (true)
114 ;
115 else {
116 }
117}
118

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-semicolon.cpp