1// RUN: %check_clang_tidy %s readability-simplify-boolean-expr %t -- -config="{CheckOptions: {readability-simplify-boolean-expr.ChainedConditionalReturn: true}}" --
2
3bool chained_conditional_compound_return(int i) {
4 if (i < 0) {
5 return true;
6 } else if (i < 10) {
7 return false;
8 } else if (i > 20) {
9 return true;
10 } else {
11 return false;
12 }
13 // CHECK-MESSAGES: :[[@LINE-4]]:12: warning: redundant boolean literal in conditional return statement [readability-simplify-boolean-expr]
14 // CHECK-FIXES: {{^}} } else if (i < 10) {{{$}}
15 // CHECK-FIXES-NEXT: {{^}} return false;{{$}}
16 // CHECK-FIXES-NEXT: {{^}} } else return i > 20;{{$}}
17}
18
19bool chained_conditional_return(int i) {
20 if (i < 0)
21 return true;
22 else if (i < 10)
23 return false;
24 else if (i > 20)
25 return true;
26 else
27 return false;
28 // CHECK-MESSAGES: :[[@LINE-3]]:12: warning: {{.*}} in conditional return statement
29 // CHECK-FIXES: {{^}} else if (i < 10)
30 // CHECK-FIXES-NEXT: {{^}} return false;
31 // CHECK-FIXES-NEXT: {{^}} else return i > 20;
32}
33
34bool chained_simple_if_return(int i) {
35 if (i < 5)
36 return true;
37 if (i > 10)
38 return true;
39 return false;
40}
41// CHECK-MESSAGES: :[[@LINE-3]]:12: warning: {{.*}} in conditional return
42// CHECK-FIXES: {{^}}bool chained_simple_if_return(int i) {{{$}}
43// CHECK-FIXES: {{^}} if (i < 5){{$}}
44// CHECK-FIXES: {{^ return true;$}}
45// CHECK-FIXES: {{^ return i > 10;$}}
46// CHECK-FIXES: {{^}$}}
47
48bool chained_simple_if_return_negated(int i) {
49 if (i < 5)
50 return false;
51 if (i > 10)
52 return false;
53 return true;
54}
55// CHECK-MESSAGES: :[[@LINE-3]]:12: warning: {{.*}} in conditional return
56// CHECK-FIXES: {{^}}bool chained_simple_if_return_negated(int i) {{{$}}
57// CHECK-FIXES: {{^}} if (i < 5){{$}}
58// CHECK-FIXES: {{^ return false;$}}
59// CHECK-FIXES: {{^ return i <= 10;$}}
60// CHECK-FIXES: {{^}$}}
61
62bool complex_chained_if_return_return(int i) {
63 if (i < 5) {
64 return true;
65 }
66 if (i > 10) {
67 return true;
68 }
69 return false;
70}
71// CHECK-MESSAGES: :[[@LINE-4]]:12: warning: {{.*}} in conditional return
72// CHECK-FIXES: {{^}}bool complex_chained_if_return_return(int i) {{{$}}
73// CHECK-FIXES: {{^}} if (i < 5) {{{$}}
74// CHECK-FIXES: {{^}} return true;{{$}}
75// CHECK-FIXES: {{^}} }{{$}}
76// CHECK-FIXES: {{^ return i > 10;$}}
77// CHECK-FIXES: {{^}$}}
78
79bool complex_chained_if_return_return_negated(int i) {
80 if (i < 5) {
81 return false;
82 }
83 if (i > 10) {
84 return false;
85 }
86 return true;
87}
88// CHECK-MESSAGES: :[[@LINE-4]]:12: warning: {{.*}} in conditional return
89// CHECK-FIXES: {{^}}bool complex_chained_if_return_return_negated(int i) {{{$}}
90// CHECK-FIXES: {{^}} if (i < 5) {{{$}}
91// CHECK-FIXES: {{^}} return false;{{$}}
92// CHECK-FIXES: {{^}} }{{$}}
93// CHECK-FIXES: {{^ return i <= 10;$}}
94// CHECK-FIXES: {{^}$}}
95
96
97bool PR57819(int x) {
98 // False positive introduced in clang-tidy-15
99 // Expect no warning here.
100 if (x > 0)
101 return false;
102 else {
103 }
104 return true;
105}
106

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