1 // RUN: %check_clang_tidy %s bugprone-redundant-branch-condition %t
2 
3 extern unsigned peopleInTheBuilding;
4 extern unsigned fireFighters;
5 
6 bool isBurning();
7 bool isReallyBurning();
8 bool isCollapsing();
9 void tryToExtinguish(bool&);
10 void tryPutFireOut();
11 bool callTheFD();
12 void scream();
13 
14 bool someOtherCondition();
15 
16 //===--- Basic Positives --------------------------------------------------===//
17 
positive_direct()18 void positive_direct() {
19   bool onFire = isBurning();
20   if (onFire) {
21     if (onFire) {
22       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
23       // CHECK-FIXES: {{^\ *$}}
24       scream();
25     }
26     // CHECK-FIXES: {{^\ *$}}
27   }
28 }
29 
positive_indirect()30 void positive_indirect() {
31   bool onFire = isBurning();
32   if (onFire) {
33     if (someOtherCondition()) {
34       if (onFire)
35         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
36         // CHECK-FIXES: {{^\ *$}}
37         scream();
38     }
39   }
40 }
41 
positive_direct_inner_and_lhs()42 void positive_direct_inner_and_lhs() {
43   bool onFire = isBurning();
44   if (onFire) {
45     if (onFire && peopleInTheBuilding > 0) {
46       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
47       // CHECK-FIXES: if ( peopleInTheBuilding > 0) {
48       scream();
49     }
50   }
51 }
52 
positive_indirect_inner_and_lhs()53 void positive_indirect_inner_and_lhs() {
54   bool onFire = isBurning();
55   if (onFire) {
56     if (someOtherCondition()) {
57       if (onFire && peopleInTheBuilding > 0) {
58         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
59         // CHECK-FIXES: if ( peopleInTheBuilding > 0) {
60         scream();
61       }
62     }
63   }
64 }
65 
positive_direct_inner_and_rhs()66 void positive_direct_inner_and_rhs() {
67   bool onFire = isBurning();
68   if (onFire) {
69     if (peopleInTheBuilding > 0 && onFire) {
70       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
71       // CHECK-FIXES: if (peopleInTheBuilding > 0 ) {
72       scream();
73     }
74   }
75 }
76 
positive_indirect_inner_and_rhs()77 void positive_indirect_inner_and_rhs() {
78   bool onFire = isBurning();
79   if (onFire) {
80     if (someOtherCondition()) {
81       if (peopleInTheBuilding > 0 && onFire) {
82         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
83         // CHECK-FIXES: if (peopleInTheBuilding > 0 ) {
84         scream();
85       }
86     }
87   }
88 }
89 
positive_direct_inner_or_lhs()90 void positive_direct_inner_or_lhs() {
91   bool onFire = isBurning();
92   if (onFire) {
93     if (onFire || isCollapsing()) {
94       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
95       // CHECK-FIXES: {{^\ *$}}
96       scream();
97     }
98     // CHECK-FIXES: {{^\ *$}}
99   }
100 }
101 
positive_indirect_inner_or_lhs()102 void positive_indirect_inner_or_lhs() {
103   bool onFire = isBurning();
104   if (onFire) {
105     if (someOtherCondition()) {
106       if (onFire || isCollapsing()) {
107         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
108         // CHECK-FIXES: {{^\ *$}}
109         scream();
110       }
111       // CHECK-FIXES: {{^\ *$}}
112     }
113   }
114 }
115 
positive_direct_inner_or_rhs()116 void positive_direct_inner_or_rhs() {
117   bool onFire = isBurning();
118   if (onFire) {
119     if (isCollapsing() || onFire) {
120       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
121       // CHECK-FIXES: {{^\ *$}}
122       scream();
123     }
124     // CHECK-FIXES: {{^\ *$}}
125   }
126 }
127 
positive_indirect_inner_or_rhs()128 void positive_indirect_inner_or_rhs() {
129   bool onFire = isBurning();
130   if (onFire) {
131     if (someOtherCondition()) {
132       if (isCollapsing() || onFire) {
133         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
134         // CHECK-FIXES: {{^\ *$}}
135         scream();
136       }
137       // CHECK-FIXES: {{^\ *$}}
138     }
139   }
140 }
141 
positive_direct_outer_and_lhs()142 void positive_direct_outer_and_lhs() {
143   bool onFire = isBurning();
144   if (onFire && fireFighters < 10) {
145     if (onFire) {
146       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
147     // CHECK-FIXES: {{^\ *$}}
148       scream();
149     }
150     // CHECK-FIXES: {{^\ *$}}
151   }
152 }
153 
positive_indirect_outer_and_lhs()154 void positive_indirect_outer_and_lhs() {
155   bool onFire = isBurning();
156   if (onFire && fireFighters < 10) {
157     if (someOtherCondition()) {
158       if (onFire) {
159         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
160         // CHECK-FIXES: {{^\ *$}}
161         scream();
162       }
163       // CHECK-FIXES: {{^\ *$}}
164     }
165   }
166 }
167 
positive_direct_outer_and_lhs_inner_and_lhs()168 void positive_direct_outer_and_lhs_inner_and_lhs() {
169   bool onFire = isBurning();
170   if (onFire && fireFighters < 10) {
171     if (onFire && peopleInTheBuilding > 0) {
172       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
173       // CHECK-FIXES: if ( peopleInTheBuilding > 0) {
174       scream();
175     }
176   }
177 }
178 
positive_indirect_outer_and_lhs_inner_and_lhs()179 void positive_indirect_outer_and_lhs_inner_and_lhs() {
180   bool onFire = isBurning();
181   if (onFire && fireFighters < 10) {
182     if (someOtherCondition()) {
183       if (onFire && peopleInTheBuilding > 0) {
184         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
185         // CHECK-FIXES: if ( peopleInTheBuilding > 0) {
186         scream();
187       }
188     }
189   }
190 }
191 
positive_direct_outer_and_lhs_inner_and_rhs()192 void positive_direct_outer_and_lhs_inner_and_rhs() {
193   bool onFire = isBurning();
194   if (onFire && fireFighters < 10) {
195     if (peopleInTheBuilding > 0 && onFire) {
196       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
197       // CHECK-FIXES: if (peopleInTheBuilding > 0 ) {
198       scream();
199     }
200   }
201 }
202 
positive_indirect_outer_and_lhs_inner_and_rhs()203 void positive_indirect_outer_and_lhs_inner_and_rhs() {
204   bool onFire = isBurning();
205   if (onFire && fireFighters < 10) {
206     if (someOtherCondition()) {
207       if (peopleInTheBuilding > 0 && onFire) {
208         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
209         // CHECK-FIXES: if (peopleInTheBuilding > 0 ) {
210         scream();
211       }
212     }
213   }
214 }
215 
positive_direct_outer_and_lhs_inner_or_lhs()216 void positive_direct_outer_and_lhs_inner_or_lhs() {
217   bool onFire = isBurning();
218   if (onFire && fireFighters < 10) {
219     if (onFire || isCollapsing()) {
220       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
221       // CHECK-FIXES: {{^\ *$}}
222       scream();
223     }
224     // CHECK-FIXES: {{^\ *$}}
225   }
226 }
227 
positive_indirect_outer_and_lhs_inner_or_lhs()228 void positive_indirect_outer_and_lhs_inner_or_lhs() {
229   bool onFire = isBurning();
230   if (onFire && fireFighters < 10) {
231     if (someOtherCondition()) {
232       if (onFire || isCollapsing()) {
233         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
234         // CHECK-FIXES: {{^\ *$}}
235         scream();
236       }
237     // CHECK-FIXES: {{^\ *$}}
238     }
239   }
240 }
241 
positive_direct_outer_and_lhs_inner_or_rhs()242 void positive_direct_outer_and_lhs_inner_or_rhs() {
243   bool onFire = isBurning();
244   if (onFire && fireFighters < 10) {
245     if (isCollapsing() || onFire) {
246       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
247       // CHECK-FIXES: {{^\ *$}}
248       scream();
249     }
250     // CHECK-FIXES: {{^\ *$}}
251   }
252 }
253 
positive_indirect_outer_and_lhs_inner_or_rhs()254 void positive_indirect_outer_and_lhs_inner_or_rhs() {
255   bool onFire = isBurning();
256   if (onFire && fireFighters < 10) {
257     if (someOtherCondition()) {
258       if (isCollapsing() || onFire) {
259         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
260         // CHECK-FIXES: {{^\ *$}}
261         scream();
262       }
263       // CHECK-FIXES: {{^\ *$}}
264     }
265   }
266 }
267 
positive_direct_outer_and_rhs()268 void positive_direct_outer_and_rhs() {
269   bool onFire = isBurning();
270   if (fireFighters < 10 && onFire) {
271     if (onFire) {
272       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
273       // CHECK-FIXES: {{^\ *$}}
274       scream();
275     }
276     // CHECK-FIXES: {{^\ *$}}
277   }
278 }
279 
positive_indirect_outer_and_rhs()280 void positive_indirect_outer_and_rhs() {
281   bool onFire = isBurning();
282   if (fireFighters < 10 && onFire) {
283     if (someOtherCondition()) {
284       if (onFire) {
285         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
286         // CHECK-FIXES: {{^\ *$}}
287         scream();
288       }
289       // CHECK-FIXES: {{^\ *$}}
290     }
291   }
292 }
293 
positive_direct_outer_and_rhs_inner_and_lhs()294 void positive_direct_outer_and_rhs_inner_and_lhs() {
295   bool onFire = isBurning();
296   if (fireFighters < 10 && onFire) {
297     if (onFire && peopleInTheBuilding > 0) {
298       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
299       // CHECK-FIXES: if ( peopleInTheBuilding > 0) {
300       scream();
301     }
302   }
303 }
304 
positive_indirect_outer_and_rhs_inner_and_lhs()305 void positive_indirect_outer_and_rhs_inner_and_lhs() {
306   bool onFire = isBurning();
307   if (fireFighters < 10 && onFire) {
308     if (someOtherCondition()) {
309       if (onFire && peopleInTheBuilding > 0) {
310         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
311         // CHECK-FIXES: if ( peopleInTheBuilding > 0) {
312         scream();
313       }
314     }
315   }
316 }
317 
positive_direct_inner_outer_and_rhs_and_rhs()318 void positive_direct_inner_outer_and_rhs_and_rhs() {
319   bool onFire = isBurning();
320   if (fireFighters < 10 && onFire) {
321     if (peopleInTheBuilding > 0 && onFire) {
322       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
323         // CHECK-FIXES: if (peopleInTheBuilding > 0 ) {
324       scream();
325     }
326   }
327 }
328 
positive_indirect_outer_and_rhs_inner_and_rhs()329 void positive_indirect_outer_and_rhs_inner_and_rhs() {
330   bool onFire = isBurning();
331   if (fireFighters < 10 && onFire) {
332     if (someOtherCondition()) {
333       if (peopleInTheBuilding > 0 && onFire) {
334         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
335         // CHECK-FIXES: if (peopleInTheBuilding > 0 ) {
336         scream();
337       }
338     }
339   }
340 }
341 
positive_direct_outer_and_rhs_inner_or_lhs()342 void positive_direct_outer_and_rhs_inner_or_lhs() {
343   bool onFire = isBurning();
344   if (fireFighters < 10 && onFire) {
345     if (onFire || isCollapsing()) {
346       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
347       // CHECK-FIXES: {{^\ *$}}
348       scream();
349     }
350     // CHECK-FIXES: {{^\ *$}}
351   }
352 }
353 
positive_indirect_outer_and_rhs_inner_or_lhs()354 void positive_indirect_outer_and_rhs_inner_or_lhs() {
355   bool onFire = isBurning();
356   if (fireFighters < 10 && onFire) {
357     if (someOtherCondition()) {
358       if (onFire || isCollapsing()) {
359         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
360         // CHECK-FIXES: {{^\ *$}}
361         scream();
362       }
363       // CHECK-FIXES: {{^\ *$}}
364     }
365   }
366 }
367 
positive_direct_outer_and_rhs_inner_or_rhs()368 void positive_direct_outer_and_rhs_inner_or_rhs() {
369   bool onFire = isBurning();
370   if (fireFighters < 10 && onFire) {
371     if (isCollapsing() || onFire) {
372       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
373       // CHECK-FIXES: {{^\ *$}}
374       scream();
375     }
376     // CHECK-FIXES: {{^\ *$}}
377   }
378 }
379 
positive_indirect_outer_and_rhs_inner_or_rhs()380 void positive_indirect_outer_and_rhs_inner_or_rhs() {
381   bool onFire = isBurning();
382   if (fireFighters < 10 && onFire) {
383     if (someOtherCondition()) {
384       if (isCollapsing() || onFire) {
385         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
386         // CHECK-FIXES: {{^\ *$}}
387         scream();
388       }
389       // CHECK-FIXES: {{^\ *$}}
390     }
391   }
392 }
393 
394 //===--- Basic Negatives --------------------------------------------------===//
395 
negative_direct()396 void negative_direct() {
397   bool onFire = isBurning();
398   if (onFire) {
399     tryToExtinguish(onFire);
400     if (onFire) {
401       // NO-MESSAGE: fire may have been extinguished
402       scream();
403     }
404   }
405 }
406 
negative_indirect()407 void negative_indirect() {
408   bool onFire = isBurning();
409   if (onFire) {
410     tryToExtinguish(onFire);
411     if (someOtherCondition()) {
412       if (onFire) {
413         // NO-MESSAGE: fire may have been extinguished
414         scream();
415       }
416     }
417   }
418 }
419 
negative_indirect2()420 void negative_indirect2() {
421   bool onFire = isBurning();
422   if (onFire) {
423     if (someOtherCondition()) {
424       tryToExtinguish(onFire);
425       if (onFire) {
426         // NO-MESSAGE: fire may have been extinguished
427         scream();
428       }
429     }
430   }
431 }
432 
negative_direct_inner_and_lhs()433 void negative_direct_inner_and_lhs() {
434   bool onFire = isBurning();
435   if (onFire) {
436     tryToExtinguish(onFire);
437     if (onFire && peopleInTheBuilding > 0) {
438       // NO-MESSAGE: fire may have been extinguished
439       scream();
440     }
441   }
442 }
443 
negative_indirect_inner_and_lhs()444 void negative_indirect_inner_and_lhs() {
445   bool onFire = isBurning();
446   if (onFire) {
447     tryToExtinguish(onFire);
448     if (someOtherCondition()) {
449       if (onFire && peopleInTheBuilding > 0) {
450         // NO-MESSAGE: fire may have been extinguished
451         scream();
452       }
453     }
454   }
455 }
456 
negative_indirect2_inner_and_lhs()457 void negative_indirect2_inner_and_lhs() {
458   bool onFire = isBurning();
459   if (onFire) {
460     if (someOtherCondition()) {
461       tryToExtinguish(onFire);
462       if (onFire && peopleInTheBuilding > 0) {
463         // NO-MESSAGE: fire may have been extinguished
464         scream();
465       }
466     }
467   }
468 }
469 
negative_direct_inner_and_rhs()470 void negative_direct_inner_and_rhs() {
471   bool onFire = isBurning();
472   if (onFire) {
473     tryToExtinguish(onFire);
474     if (peopleInTheBuilding > 0 && onFire) {
475       // NO-MESSAGE: fire may have been extinguished
476       scream();
477     }
478   }
479 }
480 
negative_indirect_inner_and_rhs()481 void negative_indirect_inner_and_rhs() {
482   bool onFire = isBurning();
483   if (onFire) {
484     tryToExtinguish(onFire);
485     if (someOtherCondition()) {
486       if (peopleInTheBuilding > 0 && onFire) {
487         // NO-MESSAGE: fire may have been extinguished
488         scream();
489       }
490     }
491   }
492 }
493 
negative_indirect2_inner_and_rhs()494 void negative_indirect2_inner_and_rhs() {
495   bool onFire = isBurning();
496   if (onFire) {
497     if (someOtherCondition()) {
498       tryToExtinguish(onFire);
499       if (peopleInTheBuilding > 0 && onFire) {
500         // NO-MESSAGE: fire may have been extinguished
501         scream();
502       }
503     }
504   }
505 }
506 
negative_direct_inner_or_lhs()507 void negative_direct_inner_or_lhs() {
508   bool onFire = isBurning();
509   if (onFire) {
510     tryToExtinguish(onFire);
511     if (onFire || isCollapsing()) {
512       // NO-MESSAGE: fire may have been extinguished
513       scream();
514     }
515   }
516 }
517 
negative_indirect_inner_or_lhs()518 void negative_indirect_inner_or_lhs() {
519   bool onFire = isBurning();
520   if (onFire) {
521     tryToExtinguish(onFire);
522     if (someOtherCondition()) {
523       if (onFire || isCollapsing()) {
524         // NO-MESSAGE: fire may have been extinguished
525         scream();
526       }
527     }
528   }
529 }
530 
negative_indirect2_inner_or_lhs()531 void negative_indirect2_inner_or_lhs() {
532   bool onFire = isBurning();
533   if (onFire) {
534     if (someOtherCondition()) {
535       tryToExtinguish(onFire);
536       if (onFire || isCollapsing()) {
537         // NO-MESSAGE: fire may have been extinguished
538         scream();
539       }
540     }
541   }
542 }
543 
negative_direct_inner_or_rhs()544 void negative_direct_inner_or_rhs() {
545   bool onFire = isBurning();
546   if (onFire) {
547     tryToExtinguish(onFire);
548     if (isCollapsing() || onFire) {
549       // NO-MESSAGE: fire may have been extinguished
550       scream();
551     }
552   }
553 }
554 
negative_indirect_inner_or_rhs()555 void negative_indirect_inner_or_rhs() {
556   bool onFire = isBurning();
557   if (onFire) {
558     tryToExtinguish(onFire);
559     if (someOtherCondition()) {
560       if (isCollapsing() || onFire) {
561         // NO-MESSAGE: fire may have been extinguished
562         scream();
563       }
564     }
565   }
566 }
567 
negative_indirect2_inner_or_rhs()568 void negative_indirect2_inner_or_rhs() {
569   bool onFire = isBurning();
570   if (onFire) {
571     if (someOtherCondition()) {
572       tryToExtinguish(onFire);
573       if (isCollapsing() || onFire) {
574         // NO-MESSAGE: fire may have been extinguished
575         scream();
576       }
577     }
578   }
579 }
580 
negative_direct_outer_and_lhs()581 void negative_direct_outer_and_lhs() {
582   bool onFire = isBurning();
583   if (onFire && fireFighters < 10) {
584     tryToExtinguish(onFire);
585     if (onFire) {
586       // NO-MESSAGE: fire may have been extinguished
587       scream();
588     }
589   }
590 }
591 
negative_indirect_outer_and_lhs()592 void negative_indirect_outer_and_lhs() {
593   bool onFire = isBurning();
594   if (onFire && fireFighters < 10) {
595     tryToExtinguish(onFire);
596     if (someOtherCondition()) {
597       if (onFire) {
598         // NO-MESSAGE: fire may have been extinguished
599         scream();
600       }
601     }
602   }
603 }
604 
negative_indirect2_outer_and_lhs()605 void negative_indirect2_outer_and_lhs() {
606   bool onFire = isBurning();
607   if (onFire && fireFighters < 10) {
608     if (someOtherCondition()) {
609       tryToExtinguish(onFire);
610       if (onFire) {
611         // NO-MESSAGE: fire may have been extinguished
612         scream();
613       }
614     }
615   }
616 }
617 
negative_direct_outer_and_lhs_inner_and_lhs()618 void negative_direct_outer_and_lhs_inner_and_lhs() {
619   bool onFire = isBurning();
620   if (onFire && fireFighters < 10) {
621     tryToExtinguish(onFire);
622     if (onFire && peopleInTheBuilding > 0) {
623       // NO-MESSAGE: fire may have been extinguished
624       scream();
625     }
626   }
627 }
628 
negative_indirect_outer_and_lhs_inner_and_lhs()629 void negative_indirect_outer_and_lhs_inner_and_lhs() {
630   bool onFire = isBurning();
631   if (onFire && fireFighters < 10) {
632     tryToExtinguish(onFire);
633     if (someOtherCondition()) {
634       if (onFire && peopleInTheBuilding > 0) {
635         // NO-MESSAGE: fire may have been extinguished
636         scream();
637       }
638     }
639   }
640 }
641 
negative_indirect2_outer_and_lhs_inner_and_lhs()642 void negative_indirect2_outer_and_lhs_inner_and_lhs() {
643   bool onFire = isBurning();
644   if (onFire && fireFighters < 10) {
645     if (someOtherCondition()) {
646       tryToExtinguish(onFire);
647       if (onFire && peopleInTheBuilding > 0) {
648         // NO-MESSAGE: fire may have been extinguished
649         scream();
650       }
651     }
652   }
653 }
654 
negative_direct_outer_and_lhs_inner_and_rhs()655 void negative_direct_outer_and_lhs_inner_and_rhs() {
656   bool onFire = isBurning();
657   if (onFire && fireFighters < 10) {
658     tryToExtinguish(onFire);
659     if (peopleInTheBuilding > 0 && onFire) {
660       // NO-MESSAGE: fire may have been extinguished
661       scream();
662     }
663   }
664 }
665 
negative_indirect_outer_and_lhs_inner_and_rhs()666 void negative_indirect_outer_and_lhs_inner_and_rhs() {
667   bool onFire = isBurning();
668   if (onFire && fireFighters < 10) {
669     tryToExtinguish(onFire);
670     if (someOtherCondition()) {
671       if (peopleInTheBuilding > 0 && onFire) {
672         // NO-MESSAGE: fire may have been extinguished
673         scream();
674       }
675     }
676   }
677 }
678 
negative_indirect2_outer_and_lhs_inner_and_rhs()679 void negative_indirect2_outer_and_lhs_inner_and_rhs() {
680   bool onFire = isBurning();
681   if (onFire && fireFighters < 10) {
682     if (someOtherCondition()) {
683       tryToExtinguish(onFire);
684       if (peopleInTheBuilding > 0 && onFire) {
685         // NO-MESSAGE: fire may have been extinguished
686         scream();
687       }
688     }
689   }
690 }
691 
negative_direct_outer_and_lhs_inner_or_lhs()692 void negative_direct_outer_and_lhs_inner_or_lhs() {
693   bool onFire = isBurning();
694   if (onFire && fireFighters < 10) {
695     tryToExtinguish(onFire);
696     if (onFire || isCollapsing()) {
697       // NO-MESSAGE: fire may have been extinguished
698       scream();
699     }
700   }
701 }
702 
negative_indirect_outer_and_lhs_inner_or_lhs()703 void negative_indirect_outer_and_lhs_inner_or_lhs() {
704   bool onFire = isBurning();
705   if (onFire && fireFighters < 10) {
706     tryToExtinguish(onFire);
707     if (someOtherCondition()) {
708       if (onFire || isCollapsing()) {
709         // NO-MESSAGE: fire may have been extinguished
710         scream();
711       }
712     }
713   }
714 }
715 
negative_indirect2_outer_and_lhs_inner_or_lhs()716 void negative_indirect2_outer_and_lhs_inner_or_lhs() {
717   bool onFire = isBurning();
718   if (onFire && fireFighters < 10) {
719     if (someOtherCondition()) {
720       tryToExtinguish(onFire);
721       if (onFire || isCollapsing()) {
722         // NO-MESSAGE: fire may have been extinguished
723         scream();
724       }
725     }
726   }
727 }
728 
negative_direct_outer_and_lhs_inner_or_rhs()729 void negative_direct_outer_and_lhs_inner_or_rhs() {
730   bool onFire = isBurning();
731   if (onFire && fireFighters < 10) {
732     tryToExtinguish(onFire);
733     if (isCollapsing() || onFire) {
734       // NO-MESSAGE: fire may have been extinguished
735       scream();
736     }
737   }
738 }
739 
negative_indirect_outer_and_lhs_inner_or_rhs()740 void negative_indirect_outer_and_lhs_inner_or_rhs() {
741   bool onFire = isBurning();
742   if (onFire && fireFighters < 10) {
743     tryToExtinguish(onFire);
744     if (someOtherCondition()) {
745       if (isCollapsing() || onFire) {
746         // NO-MESSAGE: fire may have been extinguished
747         scream();
748       }
749     }
750   }
751 }
752 
negative_indirect2_outer_and_lhs_inner_or_rhs()753 void negative_indirect2_outer_and_lhs_inner_or_rhs() {
754   bool onFire = isBurning();
755   if (onFire && fireFighters < 10) {
756     if (someOtherCondition()) {
757       tryToExtinguish(onFire);
758       if (isCollapsing() || onFire) {
759         // NO-MESSAGE: fire may have been extinguished
760         scream();
761       }
762     }
763   }
764 }
765 
negative_direct_outer_and_rhs()766 void negative_direct_outer_and_rhs() {
767   bool onFire = isBurning();
768   if (fireFighters < 10 && onFire) {
769     tryToExtinguish(onFire);
770     if (onFire) {
771       // NO-MESSAGE: fire may have been extinguished
772       scream();
773     }
774   }
775 }
776 
negative_indirect_outer_and_rhs()777 void negative_indirect_outer_and_rhs() {
778   bool onFire = isBurning();
779   if (fireFighters < 10 && onFire) {
780     tryToExtinguish(onFire);
781     if (someOtherCondition()) {
782       if (onFire) {
783         // NO-MESSAGE: fire may have been extinguished
784         scream();
785       }
786     }
787   }
788 }
789 
negative_indirect2_outer_and_rhs()790 void negative_indirect2_outer_and_rhs() {
791   bool onFire = isBurning();
792   if (fireFighters < 10 && onFire) {
793     if (someOtherCondition()) {
794       tryToExtinguish(onFire);
795       if (onFire) {
796         // NO-MESSAGE: fire may have been extinguished
797         scream();
798       }
799     }
800   }
801 }
802 
negative_direct_outer_and_rhs_inner_and_lhs()803 void negative_direct_outer_and_rhs_inner_and_lhs() {
804   bool onFire = isBurning();
805   if (fireFighters < 10 && onFire) {
806     tryToExtinguish(onFire);
807     if (onFire && peopleInTheBuilding > 0) {
808       // NO-MESSAGE: fire may have been extinguished
809       scream();
810     }
811   }
812 }
813 
negative_indirect_outer_and_rhs_inner_and_lhs()814 void negative_indirect_outer_and_rhs_inner_and_lhs() {
815   bool onFire = isBurning();
816   if (fireFighters < 10 && onFire) {
817     tryToExtinguish(onFire);
818     if (someOtherCondition()) {
819       if (onFire && peopleInTheBuilding > 0) {
820         // NO-MESSAGE: fire may have been extinguished
821         scream();
822       }
823     }
824   }
825 }
826 
negative_indirect2_outer_and_rhs_inner_and_lhs()827 void negative_indirect2_outer_and_rhs_inner_and_lhs() {
828   bool onFire = isBurning();
829   if (fireFighters < 10 && onFire) {
830     if (someOtherCondition()) {
831       tryToExtinguish(onFire);
832       if (onFire && peopleInTheBuilding > 0) {
833         // NO-MESSAGE: fire may have been extinguished
834         scream();
835       }
836     }
837   }
838 }
839 
negative_direct_inner_outer_and_rhs_and_rhs()840 void negative_direct_inner_outer_and_rhs_and_rhs() {
841   bool onFire = isBurning();
842   if (fireFighters < 10 && onFire) {
843     tryToExtinguish(onFire);
844     if (peopleInTheBuilding > 0 && onFire) {
845       // NO-MESSAGE: fire may have been extinguished
846       scream();
847     }
848   }
849 }
850 
negative_indirect_outer_and_rhs_inner_and_rhs()851 void negative_indirect_outer_and_rhs_inner_and_rhs() {
852   bool onFire = isBurning();
853   if (fireFighters < 10 && onFire) {
854     tryToExtinguish(onFire);
855     if (someOtherCondition()) {
856       if (peopleInTheBuilding > 0 && onFire) {
857         // NO-MESSAGE: fire may have been extinguished
858         scream();
859       }
860     }
861   }
862 }
863 
negative_indirect2_outer_and_rhs_inner_and_rhs()864 void negative_indirect2_outer_and_rhs_inner_and_rhs() {
865   bool onFire = isBurning();
866   if (fireFighters < 10 && onFire) {
867     if (someOtherCondition()) {
868       tryToExtinguish(onFire);
869       if (peopleInTheBuilding > 0 && onFire) {
870         // NO-MESSAGE: fire may have been extinguished
871         scream();
872       }
873     }
874   }
875 }
876 
negative_direct_outer_and_rhs_inner_or_lhs()877 void negative_direct_outer_and_rhs_inner_or_lhs() {
878   bool onFire = isBurning();
879   if (fireFighters < 10 && onFire) {
880     tryToExtinguish(onFire);
881     if (onFire || isCollapsing()) {
882       // NO-MESSAGE: fire may have been extinguished
883       scream();
884     }
885   }
886 }
887 
negative_indirect_outer_and_rhs_inner_or_lhs()888 void negative_indirect_outer_and_rhs_inner_or_lhs() {
889   bool onFire = isBurning();
890   if (fireFighters < 10 && onFire) {
891     tryToExtinguish(onFire);
892     if (someOtherCondition()) {
893       if (onFire || isCollapsing()) {
894         // NO-MESSAGE: fire may have been extinguished
895         scream();
896       }
897     }
898   }
899 }
900 
negative_indirect2_outer_and_rhs_inner_or_lhs()901 void negative_indirect2_outer_and_rhs_inner_or_lhs() {
902   bool onFire = isBurning();
903   if (fireFighters < 10 && onFire) {
904     if (someOtherCondition()) {
905       tryToExtinguish(onFire);
906       if (onFire || isCollapsing()) {
907         // NO-MESSAGE: fire may have been extinguished
908         scream();
909       }
910     }
911   }
912 }
913 
negative_direct_outer_and_rhs_inner_or_rhs()914 void negative_direct_outer_and_rhs_inner_or_rhs() {
915   bool onFire = isBurning();
916   if (fireFighters < 10 && onFire) {
917     tryToExtinguish(onFire);
918     if (isCollapsing() || onFire) {
919       // NO-MESSAGE: fire may have been extinguished
920       scream();
921     }
922   }
923 }
924 
negative_indirect_outer_and_rhs_inner_or_rhs()925 void negative_indirect_outer_and_rhs_inner_or_rhs() {
926   bool onFire = isBurning();
927   if (fireFighters < 10 && onFire) {
928     tryToExtinguish(onFire);
929     if (someOtherCondition()) {
930       if (isCollapsing() || onFire) {
931         // NO-MESSAGE: fire may have been extinguished
932         scream();
933       }
934     }
935   }
936 }
937 
negative_indirect2_outer_and_rhs_inner_or_rhs()938 void negative_indirect2_outer_and_rhs_inner_or_rhs() {
939   bool onFire = isBurning();
940   if (fireFighters < 10 && onFire) {
941     if (someOtherCondition()) {
942       tryToExtinguish(onFire);
943       if (isCollapsing() || onFire) {
944         // NO-MESSAGE: fire may have been extinguished
945         scream();
946       }
947     }
948   }
949 }
950 
negative_reassigned()951 void negative_reassigned() {
952   bool onFire = isBurning();
953   if (onFire) {
954     onFire = isReallyBurning();
955     if (onFire) {
956       // NO-MESSAGE: it was a false alarm then
957       scream();
958     }
959   }
960 }
961 
962 //===--- Special Positives ------------------------------------------------===//
963 
964 // Condition variable mutated in or after the inner loop
965 
positive_direct_mutated_after_inner()966 void positive_direct_mutated_after_inner() {
967   bool onFire = isBurning();
968   if (onFire) {
969     if (onFire) {
970       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
971       // CHECK-FIXES: {{^\ *$}}
972       scream();
973     }
974     // CHECK-FIXES: {{^\ *$}}
975     tryToExtinguish(onFire);
976   }
977 }
978 
positive_indirect_mutated_after_inner()979 void positive_indirect_mutated_after_inner() {
980   bool onFire = isBurning();
981   if (onFire) {
982     if (someOtherCondition()) {
983       if (onFire) {
984         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
985         // CHECK-FIXES: {{^\ *$}}
986         scream();
987       }
988       // CHECK-FIXES: {{^\ *$}}
989     }
990     tryToExtinguish(onFire);
991   }
992 }
993 
positive_indirect2_mutated_after_inner()994 void positive_indirect2_mutated_after_inner() {
995   bool onFire = isBurning();
996   if (onFire) {
997     if (someOtherCondition()) {
998       if (onFire) {
999         // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
1000         // CHECK-FIXES: {{^\ *$}}
1001         scream();
1002       }
1003       // CHECK-FIXES: {{^\ *$}}
1004       tryToExtinguish(onFire);
1005     }
1006   }
1007 }
1008 
positive_mutated_in_inner()1009 void positive_mutated_in_inner() {
1010   bool onFire = isBurning();
1011   if (onFire) {
1012     if (onFire) {
1013       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
1014       // CHECK-FIXES: {{^\ *$}}
1015       tryToExtinguish(onFire);
1016       scream();
1017     }
1018     // CHECK-FIXES: {{^\ *$}}
1019   }
1020 }
1021 
positive_or_lhs_with_side_effect()1022 void positive_or_lhs_with_side_effect() {
1023   bool onFire = isBurning();
1024   if (onFire) {
1025     if (callTheFD() || onFire) {
1026       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
1027       // CHECK-FIXES: callTheFD() ;
1028       scream();
1029     }
1030     // CHECK-FIXES: {{^\ *$}}
1031   }
1032 }
1033 
positive_or_rhs_with_side_effect()1034 void positive_or_rhs_with_side_effect() {
1035   bool onFire = isBurning();
1036   if (onFire) {
1037     if (onFire || callTheFD()) {
1038       // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
1039       // CHECK-FIXES: callTheFD();
1040       scream();
1041     }
1042     // CHECK-FIXES: {{^\ *$}}
1043   }
1044 }
1045 
1046 // GNU Expression Statements
1047 
1048 void doSomething();
1049 
positive_gnu_expression_statement()1050 void positive_gnu_expression_statement() {
1051   bool onFire = isBurning();
1052   if (({ doSomething(); onFire; })) {
1053     if (({ doSomething(); onFire; })) {
1054       // FIXME: Handle GNU epxression statements
1055       // CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
1056       // CHCK-FIXES-NOT: doSomething();
1057       scream();
1058     }
1059   }
1060 }
1061 
1062 // Comma after Condition
1063 
positive_comma_after_condition()1064 void positive_comma_after_condition() {
1065   bool onFire = isBurning();
1066   if (doSomething(), onFire) {
1067     if (doSomething(), onFire) {
1068       // FIXME: Handle comma operator
1069       // CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
1070       // CHCK-FIXES-NOT: doSomething();
1071       scream();
1072     }
1073   }
1074 }
1075 
1076 //===--- Special Negatives ------------------------------------------------===//
1077 
1078 // Aliasing
1079 
negative_mutated_by_ptr()1080 void negative_mutated_by_ptr() {
1081   bool onFire = isBurning();
1082   bool *firePtr = &onFire;
1083   if (onFire) {
1084     tryToExtinguish(*firePtr);
1085     if (onFire) {
1086       // NO-MESSAGE: fire may have been extinguished
1087       scream();
1088     }
1089   }
1090 }
1091 
negative_mutated_by_ref()1092 void negative_mutated_by_ref() {
1093   bool onFire = isBurning();
1094   bool &fireRef = onFire;
1095   if (onFire) {
1096     tryToExtinguish(fireRef);
1097     if (onFire) {
1098       // NO-MESSAGE: fire may have been extinguished
1099       scream();
1100     }
1101   }
1102 }
1103 
1104 // Volatile
1105 
negatvie_volatile()1106 void negatvie_volatile() {
1107   bool volatile onFire = isBurning();
1108   if (onFire) {
1109     if (onFire) {
1110       // NO-MESSAGE: maybe some other thread extinguished the fire
1111       scream();
1112     }
1113   }
1114 }
1115 
negative_else_branch(bool isHot)1116 void negative_else_branch(bool isHot) {
1117   bool onFire = isBurning();
1118   if (onFire) {
1119     tryPutFireOut();
1120   } else {
1121     if (isHot && onFire) {
1122       // NO-MESSAGE: new check is in the `else` branch
1123       // FIXME: handle `else` branches and negated conditions
1124       scream();
1125     }
1126   }
1127 }
1128 
1129 // GNU Expression Statements
1130 
negative_gnu_expression_statement()1131 void negative_gnu_expression_statement() {
1132   bool onFire = isBurning();
1133   if (({ doSomething(); onFire; })) {
1134     tryToExtinguish(onFire);
1135     if (({ doSomething(); onFire; })) {
1136       // NO-MESSAGE: fire may have been extinguished
1137       scream();
1138     }
1139   }
1140 }
1141 
1142 // Comma after Condition
1143 
negative_comma_after_condition()1144 void negative_comma_after_condition() {
1145   bool onFire = isBurning();
1146   if (doSomething(), onFire) {
1147     tryToExtinguish(onFire);
1148     if (doSomething(), onFire) {
1149       // NO-MESSAGE: fire may have been extinguished
1150       scream();
1151     }
1152   }
1153 }
1154 
1155 //===--- Unhandled Cases --------------------------------------------------===//
1156 
negated_in_else()1157 void negated_in_else() {
1158   bool onFire = isBurning();
1159   if (onFire) {
1160     scream();
1161   } else {
1162     if (!onFire) {
1163       doSomething();
1164     }
1165   }
1166 }
1167 
equality()1168 void equality() {
1169   if (peopleInTheBuilding == 1) {
1170     if (peopleInTheBuilding == 1) {
1171       doSomething();
1172     }
1173   }
1174 }
1175 
relational_operator()1176 void relational_operator() {
1177   if (peopleInTheBuilding > 2) {
1178     if (peopleInTheBuilding > 1) {
1179       doSomething();
1180     }
1181   }
1182 }
1183 
relational_operator_reversed()1184 void relational_operator_reversed() {
1185   if (peopleInTheBuilding > 1) {
1186     if (1 < peopleInTheBuilding) {
1187       doSomething();
1188     }
1189   }
1190 }
1191