• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %check_clang_tidy %s readability-braces-around-statements %t
2 
do_something(const char *)3 void do_something(const char *) {}
4 
cond(const char *)5 bool cond(const char *) {
6   return false;
7 }
8 
9 #define EMPTY_MACRO
10 #define EMPTY_MACRO_FUN()
11 
test()12 void test() {
13   if (cond("if0") /*comment*/) do_something("same-line");
14   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: statement should be inside braces
15   // CHECK-FIXES:   if (cond("if0") /*comment*/) { do_something("same-line");
16   // CHECK-FIXES: }
17 
18   if (cond("if0.1"))
19     do_something("single-line");
20   // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: statement should be inside braces
21   // CHECK-FIXES: if (cond("if0.1")) {
22   // CHECK-FIXES: }
23 
24   if (cond("if1") /*comment*/)
25     // some comment
26     do_something("if1");
27   // CHECK-MESSAGES: :[[@LINE-3]]:31: warning: statement should be inside braces
28   // CHECK-FIXES: if (cond("if1") /*comment*/) {
29   // CHECK-FIXES: }
30   if (cond("if2")) {
31     do_something("if2");
32   }
33   if (cond("if3"))
34     ;
35   // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces
36   // CHECK-FIXES: if (cond("if3")) {
37   // CHECK-FIXES: }
38 
39   if (cond("if-else1"))
40     do_something("if-else1");
41   // CHECK-MESSAGES: :[[@LINE-2]]:24: warning: statement should be inside braces
42   // CHECK-FIXES: if (cond("if-else1")) {
43   // CHECK-FIXES: } else {
44   else
45     do_something("if-else1 else");
46   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: statement should be inside braces
47   // CHECK-FIXES: }
48   if (cond("if-else2")) {
49     do_something("if-else2");
50   } else {
51     do_something("if-else2 else");
52   }
53 
54   if (cond("if-else if-else1"))
55     do_something("if");
56   // CHECK-MESSAGES: :[[@LINE-2]]:32: warning: statement should be inside braces
57   // CHECK-FIXES: } else if (cond("else if1")) {
58   else if (cond("else if1"))
59     do_something("else if");
60   // CHECK-MESSAGES: :[[@LINE-2]]:29: warning: statement should be inside braces
61   else
62     do_something("else");
63   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: statement should be inside braces
64   // CHECK-FIXES: }
65   if (cond("if-else if-else2")) {
66     do_something("if");
67   } else if (cond("else if2")) {
68     do_something("else if");
69   } else {
70     do_something("else");
71   }
72 
73   for (;;)
74     do_something("for");
75   // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: statement should be inside braces
76   // CHECK-FIXES: for (;;) {
77   // CHECK-FIXES: }
78   for (;;) {
79     do_something("for");
80   }
81   for (;;)
82     ;
83   // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: statement should be inside braces
84   // CHECK-FIXES: for (;;) {
85   // CHECK-FIXES: }
86 
87   int arr[4] = {1, 2, 3, 4};
88   for (int a : arr)
89     do_something("for-range");
90   // CHECK-MESSAGES: :[[@LINE-2]]:20: warning: statement should be inside braces
91   // CHECK-FIXES: for (int a : arr) {
92   // CHECK-FIXES: }
93   for (int a : arr) {
94     do_something("for-range");
95   }
96   for (int a : arr)
97     ;
98   // CHECK-MESSAGES: :[[@LINE-2]]:20: warning: statement should be inside braces
99   // CHECK-FIXES: for (int a : arr) {
100   // CHECK-FIXES: }
101 
102   while (cond("while1"))
103     do_something("while");
104   // CHECK-MESSAGES: :[[@LINE-2]]:25: warning: statement should be inside braces
105   // CHECK-FIXES: while (cond("while1")) {
106   // CHECK-FIXES: }
107   while (cond("while2")) {
108     do_something("while");
109   }
110   while (false)
111     ;
112   // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: statement should be inside braces
113   // CHECK-FIXES: while (false) {
114   // CHECK-FIXES: }
115 
116   do
117     do_something("do1");
118   while (cond("do1"));
119   // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: statement should be inside braces
120   // CHECK-FIXES: do {
121   // CHECK-FIXES: } while (cond("do1"));
122   do {
123     do_something("do2");
124   } while (cond("do2"));
125 
126   do
127     ;
128   while (false);
129   // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: statement should be inside braces
130   // CHECK-FIXES: do {
131   // CHECK-FIXES: } while (false);
132 
133   if (cond("ifif1"))
134     // comment
135     if (cond("ifif2"))
136       // comment
137       /*comment*/ ; // comment
138   // CHECK-MESSAGES: :[[@LINE-5]]:21: warning: statement should be inside braces
139   // CHECK-MESSAGES: :[[@LINE-4]]:23: warning: statement should be inside braces
140   // CHECK-FIXES: if (cond("ifif1")) {
141   // CHECK-FIXES: if (cond("ifif2")) {
142   // CHECK-FIXES: }
143   // CHECK-FIXES-NEXT: }
144 
145   if (cond("ifif3"))
146     // comment
147     if (cond("ifif4")) {
148       // comment
149       /*comment*/; // comment
150     }
151   // CHECK-MESSAGES: :[[@LINE-6]]:21: warning: statement should be inside braces
152   // CHECK-FIXES: if (cond("ifif3")) {
153   // CHECK-FIXES: }
154 
155   if (cond("ifif5"))
156     ; /* multi-line
157         comment */
158   // CHECK-MESSAGES: :[[@LINE-3]]:21: warning: statement should be inside braces
159   // CHECK-FIXES: if (cond("ifif5")) {
160   // CHECK-FIXES: }/* multi-line
161 
162   if (1) while (2) if (3) for (;;) do ; while(false) /**/;/**/
163   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should be inside braces
164   // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces
165   // CHECK-MESSAGES: :[[@LINE-3]]:26: warning: statement should be inside braces
166   // CHECK-MESSAGES: :[[@LINE-4]]:35: warning: statement should be inside braces
167   // CHECK-MESSAGES: :[[@LINE-5]]:38: warning: statement should be inside braces
168   // CHECK-FIXES: if (1) { while (2) { if (3) { for (;;) { do { ; } while(false) /**/;/**/
169   // CHECK-FIXES-NEXT: }
170   // CHECK-FIXES-NEXT: }
171   // CHECK-FIXES-NEXT: }
172   // CHECK-FIXES-NEXT: }
173 }
174 
f(const char * p)175 void f(const char *p) {
176   if (!p)
177     f("\
178 ");
179 } // end of f
180 // CHECK-MESSAGES: :[[@LINE-4]]:10: warning: statement should be inside braces
181 // CHECK-FIXES:      {{^}}  if (!p) {{{$}}
182 // CHECK-FIXES-NEXT: {{^}}    f("\{{$}}
183 // CHECK-FIXES-NEXT: {{^}}");{{$}}
184 // CHECK-FIXES-NEXT: {{^}}}{{$}}
185 // CHECK-FIXES-NEXT: {{^}}} // end of f{{$}}
186 
187 #define M(x) x
188 
test_macros(bool b)189 int test_macros(bool b) {
190   if (b) {
191     return 1;
192   } else
193     M(return 2);
194   // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: statement should be inside braces
195   // CHECK-FIXES: } else {
196   // CHECK-FIXES-NEXT:   M(return 2);
197   // CHECK-FIXES-NEXT: }
198   M(
199     for (;;)
200       ;
201   );
202   // CHECK-MESSAGES: :[[@LINE-3]]:13: warning: statement should be inside braces
203   // CHECK-FIXES: {{^}}    for (;;) {{{$}}
204   // CHECK-FIXES-NEXT: {{^      ;$}}
205   // CHECK-FIXES-NEXT: {{^}$}}
206 }
207