1// RUN: %check_clang_tidy %s readability-simplify-boolean-expr %t
2
3bool switch_stmt(int i, int j, bool b) {
4 switch (i) {
5 case 0:
6 if (b == true)
7 j = 10;
8 break;
9 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
10 // CHECK-FIXES: {{if \(b\)}}
11 // CHECK-FIXES-NEXT: {{j = 10;}}
12
13 case 1:
14 if (b == false)
15 j = -20;
16 break;
17 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
18 // CHECK-FIXES: {{if \(!b\)}}
19 // CHECK-FIXES-NEXT: {{j = -20;}}
20
21 case 2:
22 if (b && true)
23 j = 10;
24 break;
25 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
26 // CHECK-FIXES: {{if \(b\)}}
27 // CHECK-FIXES-NEXT: {{j = 10;}}
28
29 case 3:
30 if (b && false)
31 j = -20;
32 break;
33 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
34 // CHECK-FIXES: {{if \(false\)}}
35 // CHECK-FIXES-NEXT: {{j = -20;}}
36
37 case 4:
38 if (b || true)
39 j = 10;
40 break;
41 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
42 // CHECK-FIXES: {{if \(true\)}}
43 // CHECK-FIXES-NEXT: {{j = 10;}}
44 // CHECK-FIXES-NEXT: {{break;}}
45
46 case 5:
47 if (b || false)
48 j = -20;
49 break;
50 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
51 // CHECK-FIXES: {{if \(b\)}}
52 // CHECK-FIXES-NEXT: {{j = -20;}}
53
54 case 6:
55 return i > 0 ? true : false;
56 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}} in ternary expression result
57 // CHECK-FIXES: {{return i > 0;}}
58
59 case 7:
60 return i > 0 ? false : true;
61 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}} in ternary expression result
62 // CHECK-FIXES: {{return i <= 0;}}
63
64 case 8:
65 if (true)
66 j = 10;
67 else
68 j = -20;
69 break;
70 // CHECK-MESSAGES: :[[@LINE-5]]:9: warning: {{.*}} in if statement condition
71 // CHECK-FIXES: {{j = 10;$}}
72 // CHECK-FIXES-NEXT: {{break;$}}
73
74 case 9:
75 if (false)
76 j = -20;
77 else
78 j = 10;
79 break;
80 // CHECK-MESSAGES: :[[@LINE-5]]:9: warning: {{.*}} in if statement condition
81 // CHECK-FIXES: {{j = 10;}}
82 // CHECK-FIXES-NEXT: {{break;}}
83
84 case 10:
85 if (j > 10)
86 return true;
87 else
88 return false;
89 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} in conditional return statement
90 // CHECK-FIXES: {{return j > 10;}}
91
92 case 11:
93 if (j > 10)
94 return false;
95 else
96 return true;
97 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} in conditional return statement
98 // CHECK-FIXES: {{return j <= 10;}}
99
100 case 12:
101 if (j > 10)
102 b = true;
103 else
104 b = false;
105 return b;
106 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: {{.*}} in conditional assignment
107 // CHECK-FIXES: {{b = j > 10;}}
108 // CHECK-FIXES-NEXT: {{return b;}}
109
110 case 13:
111 if (j > 10)
112 b = false;
113 else
114 b = true;
115 return b;
116 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: {{.*}} in conditional assignment
117 // CHECK-FIXES: {{b = j <= 10;}}
118 // CHECK-FIXES-NEXT: {{return b;}}
119
120 case 14:
121 if (j > 10)
122 return true;
123 return false;
124 // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: {{.*}} in conditional return
125 // FIXES: {{return j > 10;}}
126
127 case 15:
128 if (j > 10)
129 return false;
130 return true;
131 // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: {{.*}} in conditional return
132 // FIXES: {{return j <= 10;}}
133
134 case 16:
135 if (j > 10)
136 return true;
137 return true;
138 return false;
139
140 case 17:
141 if (j > 10)
142 return false;
143 return false;
144 return true;
145
146 case 100: {
147 if (b == true)
148 j = 10;
149 break;
150 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
151 // CHECK-FIXES: {{if \(b\)}}
152 // CHECK-FIXES-NEXT: {{j = 10;}}
153 }
154
155 case 101: {
156 if (b == false)
157 j = -20;
158 break;
159 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
160 // CHECK-FIXES: {{if \(!b\)}}
161 // CHECK-FIXES-NEXT: {{j = -20;}}
162 }
163
164 case 102: {
165 if (b && true)
166 j = 10;
167 break;
168 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
169 // CHECK-FIXES: {{if \(b\)}}
170 // CHECK-FIXES-NEXT: {{j = 10;}}
171 }
172
173 case 103: {
174 if (b && false)
175 j = -20;
176 break;
177 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
178 // CHECK-FIXES: {{if \(false\)}}
179 // CHECK-FIXES-NEXT: {{j = -20;}}
180 }
181
182 case 104: {
183 if (b || true)
184 j = 10;
185 break;
186 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
187 // CHECK-FIXES: {{if \(true\)}}
188 // CHECK-FIXES-NEXT: {{j = 10;}}
189 // CHECK-FIXES-NEXT: {{break;}}
190 }
191
192 case 105: {
193 if (b || false)
194 j = -20;
195 break;
196 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
197 // CHECK-FIXES: {{if \(b\)}}
198 // CHECK-FIXES-NEXT: {{j = -20;}}
199 }
200
201 case 106: {
202 return i > 0 ? true : false;
203 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}} in ternary expression result
204 // CHECK-FIXES: {{return i > 0;}}
205 }
206
207 case 107: {
208 return i > 0 ? false : true;
209 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}} in ternary expression result
210 // CHECK-FIXES: {{return i <= 0;}}
211 }
212
213 case 108: {
214 if (true)
215 j = 10;
216 else
217 j = -20;
218 break;
219 // CHECK-MESSAGES: :[[@LINE-5]]:9: warning: {{.*}} in if statement condition
220 // CHECK-FIXES: {{j = 10;$}}
221 // CHECK-FIXES-NEXT: {{break;$}}
222 }
223
224 case 109: {
225 if (false)
226 j = -20;
227 else
228 j = 10;
229 break;
230 // CHECK-MESSAGES: :[[@LINE-5]]:9: warning: {{.*}} in if statement condition
231 // CHECK-FIXES: {{j = 10;}}
232 // CHECK-FIXES-NEXT: {{break;}}
233 }
234
235 case 110: {
236 if (j > 10)
237 return true;
238 else
239 return false;
240 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} in conditional return statement
241 // CHECK-FIXES: {{return j > 10;}}
242 }
243
244 case 111: {
245 if (j > 10)
246 return false;
247 else
248 return true;
249 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} in conditional return statement
250 // CHECK-FIXES: {{return j <= 10;}}
251 }
252
253 case 112: {
254 if (j > 10)
255 b = true;
256 else
257 b = false;
258 return b;
259 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: {{.*}} in conditional assignment
260 // CHECK-FIXES: {{b = j > 10;}}
261 // CHECK-FIXES-NEXT: {{return b;}}
262 }
263
264 case 113: {
265 if (j > 10)
266 b = false;
267 else
268 b = true;
269 return b;
270 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: {{.*}} in conditional assignment
271 // CHECK-FIXES: {{b = j <= 10;}}
272 // CHECK-FIXES-NEXT: {{return b;}}
273 }
274
275 case 114: {
276 if (j > 10)
277 return true;
278 return false;
279 // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: {{.*}} in conditional return
280 // CHECK-FIXES: {{return j > 10;}}
281 }
282
283 case 115: {
284 if (j > 10)
285 return false;
286 return true;
287 // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: {{.*}} in conditional return
288 // CHECK-FIXES: {{return j <= 10;}}
289 }
290
291 case 116: {
292 return false;
293 if (j > 10)
294 return true;
295 }
296
297 case 117: {
298 return true;
299 if (j > 10)
300 return false;
301 }
302 }
303
304 return j > 0;
305}
306
307bool default_stmt0(int i, int j, bool b) {
308 switch (i) {
309 case 0:
310 return true;
311
312 default:
313 if (b == true)
314 j = 10;
315 break;
316 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
317 // CHECK-FIXES: {{if \(b\)}}
318 // CHECK-FIXES-NEXT: {{j = 10;}}
319 }
320 return false;
321}
322
323bool default_stmt1(int i, int j, bool b) {
324 switch (i) {
325 case 0:
326 return true;
327
328 default:
329 if (b == false)
330 j = -20;
331 break;
332 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
333 // CHECK-FIXES: {{if \(!b\)}}
334 // CHECK-FIXES-NEXT: {{j = -20;}}
335 }
336 return false;
337}
338
339bool default_stmt2(int i, int j, bool b) {
340 switch (i) {
341 case 0:
342 return true;
343
344 default:
345 if (b && true)
346 j = 10;
347 break;
348 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
349 // CHECK-FIXES: {{if \(b\)}}
350 // CHECK-FIXES-NEXT: {{j = 10;}}
351 }
352 return false;
353}
354
355bool default_stmt3(int i, int j, bool b) {
356 switch (i) {
357 case 0:
358 return true;
359
360 default:
361 if (b && false)
362 j = -20;
363 break;
364 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
365 // CHECK-FIXES: {{if \(false\)}}
366 // CHECK-FIXES-NEXT: {{j = -20;}}
367 }
368 return false;
369}
370
371bool default_stmt4(int i, int j, bool b) {
372 switch (i) {
373 case 0:
374 return true;
375
376 default:
377 if (b || true)
378 j = 10;
379 break;
380 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
381 // CHECK-FIXES: {{if \(true\)}}
382 // CHECK-FIXES-NEXT: {{j = 10;}}
383 // CHECK-FIXES-NEXT: {{break;}}
384 }
385 return false;
386}
387
388bool default_stmt5(int i, int j, bool b) {
389 switch (i) {
390 case 0:
391 return true;
392
393 default:
394 if (b || false)
395 j = -20;
396 break;
397 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} to boolean operator
398 // CHECK-FIXES: {{if \(b\)}}
399 // CHECK-FIXES-NEXT: {{j = -20;}}
400 }
401 return false;
402}
403
404bool default_stmt6(int i, int j, bool b) {
405 switch (i) {
406 case 0:
407 return true;
408
409 default:
410 return i > 0 ? true : false;
411 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}} in ternary expression result
412 // CHECK-FIXES: {{return i > 0;}}
413 }
414 return false;
415}
416
417bool default_stmt7(int i, int j, bool b) {
418 switch (i) {
419 case 0:
420 return true;
421
422 default:
423 return i > 0 ? false : true;
424 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}} in ternary expression result
425 // CHECK-FIXES: {{return i <= 0;}}
426 }
427 return false;
428}
429
430bool default_stmt8(int i, int j, bool b) {
431 switch (i) {
432 case 0:
433 return true;
434
435 default:
436 if (true)
437 j = 10;
438 else
439 j = -20;
440 break;
441 // CHECK-MESSAGES: :[[@LINE-5]]:9: warning: {{.*}} in if statement condition
442 // CHECK-FIXES: {{j = 10;$}}
443 // CHECK-FIXES-NEXT: {{break;$}}
444 }
445 return false;
446}
447
448bool default_stmt9(int i, int j, bool b) {
449 switch (i) {
450 case 0:
451 return true;
452
453 default:
454 if (false)
455 j = -20;
456 else
457 j = 10;
458 break;
459 // CHECK-MESSAGES: :[[@LINE-5]]:9: warning: {{.*}} in if statement condition
460 // CHECK-FIXES: {{j = 10;}}
461 // CHECK-FIXES-NEXT: {{break;}}
462 }
463 return false;
464}
465
466bool default_stmt10(int i, int j, bool b) {
467 switch (i) {
468 case 0:
469 return true;
470
471 default:
472 if (j > 10)
473 return true;
474 else
475 return false;
476 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} in conditional return statement
477 // CHECK-FIXES: {{return j > 10;}}
478 }
479 return false;
480}
481
482bool default_stmt11(int i, int j, bool b) {
483 switch (i) {
484 case 0:
485 return true;
486
487 default:
488 if (j > 10)
489 return false;
490 else
491 return true;
492 // CHECK-MESSAGES: :[[@LINE-3]]:14: warning: {{.*}} in conditional return statement
493 // CHECK-FIXES: {{return j <= 10;}}
494 }
495 return false;
496}
497
498bool default_stmt12(int i, int j, bool b) {
499 switch (i) {
500 case 0:
501 return true;
502
503 default:
504 if (j > 10)
505 b = true;
506 else
507 b = false;
508 return b;
509 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: {{.*}} in conditional assignment
510 // CHECK-FIXES: {{b = j > 10;}}
511 // CHECK-FIXES-NEXT: {{return b;}}
512 }
513 return false;
514}
515
516bool default_stmt13(int i, int j, bool b) {
517 switch (i) {
518 case 0:
519 return true;
520
521 default:
522 if (j > 10)
523 b = false;
524 else
525 b = true;
526 return b;
527 // CHECK-MESSAGES: :[[@LINE-4]]:11: warning: {{.*}} in conditional assignment
528 // CHECK-FIXES: {{b = j <= 10;}}
529 // CHECK-FIXES-NEXT: {{return b;}}
530 }
531 return false;
532}
533
534bool default_stmt14(int i, int j, bool b) {
535 switch (i) {
536 case 0:
537 return true;
538
539 default:
540 if (j > 10)
541 return true;
542 return false;
543 // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: {{.*}} in conditional return
544 // FIXES: {{return j > 10;}}
545 }
546 return false;
547}
548
549bool default_stmt15(int i, int j, bool b) {
550 switch (i) {
551 case 0:
552 return true;
553
554 default:
555 if (j > 10)
556 return false;
557 return true;
558 // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: {{.*}} in conditional return
559 // FIXES: {{return j <= 10;}}
560 }
561 return false;
562}
563
564bool default_stmt16(int i, int j, bool b) {
565 switch (i) {
566 case 0:
567 return false;
568
569 default:
570 if (j > 10)
571 return true;
572 }
573 return false;
574}
575
576bool default_stmt17(int i, int j, bool b) {
577 switch (i) {
578 case 0:
579 return true;
580
581 default:
582 if (j > 10)
583 return false;
584 }
585 return false;
586}
587
588bool label_stmt0(int i, int j, bool b) {
589label:
590 if (b == true)
591 j = 10;
592 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} to boolean operator
593 // CHECK-FIXES: {{if \(b\)}}
594 // CHECK-FIXES-NEXT: {{j = 10;}}
595 return false;
596}
597
598bool label_stmt1(int i, int j, bool b) {
599label:
600 if (b == false)
601 j = -20;
602 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} to boolean operator
603 // CHECK-FIXES: {{if \(!b\)}}
604 // CHECK-FIXES-NEXT: {{j = -20;}}
605 return false;
606}
607
608bool label_stmt2(int i, int j, bool b) {
609label:
610 if (b && true)
611 j = 10;
612 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} to boolean operator
613 // CHECK-FIXES: {{if \(b\)}}
614 // CHECK-FIXES-NEXT: {{j = 10;}}
615 return false;
616}
617
618bool label_stmt3(int i, int j, bool b) {
619label:
620 if (b && false)
621 j = -20;
622 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} to boolean operator
623 // CHECK-FIXES: {{if \(false\)}}
624 // CHECK-FIXES-NEXT: {{j = -20;}}
625 return false;
626}
627
628bool label_stmt4(int i, int j, bool b) {
629label:
630 if (b || true)
631 j = 10;
632 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} to boolean operator
633 // CHECK-FIXES: {{if \(true\)}}
634 // CHECK-FIXES-NEXT: {{j = 10;}}
635 return false;
636}
637
638bool label_stmt5(int i, int j, bool b) {
639label:
640 if (b || false)
641 j = -20;
642 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} to boolean operator
643 // CHECK-FIXES: {{if \(b\)}}
644 // CHECK-FIXES-NEXT: {{j = -20;}}
645 return false;
646}
647
648bool label_stmt6(int i, int j, bool b) {
649label:
650 return i > 0 ? true : false;
651 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}} in ternary expression result
652 // CHECK-FIXES: {{return i > 0;}}
653}
654
655bool label_stmt7(int i, int j, bool b) {
656label:
657 return i > 0 ? false : true;
658 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}} in ternary expression result
659 // CHECK-FIXES: {{return i <= 0;}}
660}
661
662bool label_stmt8(int i, int j, bool b) {
663label:
664 if (true)
665 j = 10;
666 else
667 j = -20;
668 // CHECK-MESSAGES: :[[@LINE-4]]:7: warning: {{.*}} in if statement condition
669 // CHECK-FIXES: {{j = 10;$}}
670 return false;
671}
672
673bool label_stmt9(int i, int j, bool b) {
674label:
675 if (false)
676 j = -20;
677 else
678 j = 10;
679 // CHECK-MESSAGES: :[[@LINE-4]]:7: warning: {{.*}} in if statement condition
680 // CHECK-FIXES: {{j = 10;}}
681 return false;
682}
683
684bool label_stmt10(int i, int j, bool b) {
685label:
686 if (j > 10)
687 return true;
688 else
689 return false;
690 // CHECK-MESSAGES: :[[@LINE-3]]:12: warning: {{.*}} in conditional return statement
691 // CHECK-FIXES: {{return j > 10;}}
692}
693
694bool label_stmt11(int i, int j, bool b) {
695label:
696 if (j > 10)
697 return false;
698 else
699 return true;
700 // CHECK-MESSAGES: :[[@LINE-3]]:12: warning: {{.*}} in conditional return statement
701 // CHECK-FIXES: {{return j <= 10;}}
702}
703
704bool label_stmt12(int i, int j, bool b) {
705label:
706 if (j > 10)
707 b = true;
708 else
709 b = false;
710 return b;
711 // CHECK-MESSAGES: :[[@LINE-4]]:9: warning: {{.*}} in conditional assignment
712 // CHECK-FIXES: {{b = j > 10;}}
713 // CHECK-FIXES-NEXT: {{return b;}}
714}
715
716bool label_stmt13(int i, int j, bool b) {
717label:
718 if (j > 10)
719 b = false;
720 else
721 b = true;
722 return b;
723 // CHECK-MESSAGES: :[[@LINE-4]]:9: warning: {{.*}} in conditional assignment
724 // CHECK-FIXES: {{b = j <= 10;}}
725 // CHECK-FIXES-NEXT: {{return b;}}
726}
727
728bool label_stmt14(int i, int j, bool b) {
729label:
730 if (j > 10)
731 return true;
732 return false;
733 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} in conditional return
734 // FIXES: {{return j > 10;}}
735}
736
737bool label_stmt15(int i, int j, bool b) {
738label:
739 if (j > 10)
740 return false;
741 return true;
742 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: {{.*}} in conditional return
743 // FIXES: {{return j <= 10;}}
744}
745

source code of clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-case.cpp