File tree Expand file tree Collapse file tree 2 files changed +29
-6
lines changed
Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,12 @@ Examples:
8080 ``if (x) return true; return false; ``
8181 becomes ``return static_cast<bool>(x); ``
8282
83+ .. note ::
84+
85+ This check properly handles C++17 ``if `` statements with initialization
86+ statements (e.g. ``if (int i = 0; i) ... ``). It will not report false positives
87+ or suggest invalid fixes for the condition variable in these cases.
88+
8389Options
8490-------
8591
Original file line number Diff line number Diff line change @@ -1036,6 +1036,14 @@ void instantiate() {
10361036 ignoreInstantiations<false >();
10371037}
10381038
1039+ void test_init_stmt_true () {
1040+ void foo (int i);
1041+ if (int i = 0 ; true )
1042+ foo (i);
1043+ // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: redundant boolean literal in if statement condition [readability-simplify-boolean-expr]
1044+ // CHECK-FIXES: { int i = 0;foo(i) };
1045+ }
1046+
10391047void if_with_init_statement () {
10401048 bool x = true ;
10411049 if (bool y = x; y == true ) {
@@ -1044,10 +1052,19 @@ void if_with_init_statement() {
10441052 }
10451053}
10461054
1047- void test_init_stmt_true () {
1048- void foo (int i);
1049- if (int i = 0 ; true )
1050- foo (i);
1051- // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: redundant boolean literal in if statement condition [readability-simplify-boolean-expr]
1052- // CHECK-FIXES: { int i = 0;foo(i) };
1055+ // This matches the "RAII" and "Cond" logic from the deleted C++17 file
1056+ // to ensure we don't regress or crash on these complex cases.
1057+ void test_cxx17_raii_and_complex () {
1058+ struct RAII {};
1059+ bool Cond = true ;
1060+ // Case 1: Init statement with non-boolean type
1061+ if (RAII Object; Cond) {
1062+ // No warning expected here for now, just verifying no crash.
1063+ }
1064+ // Case 2: Init statement declaring the condition variable
1065+ if (bool X = Cond; X) {
1066+ // No warning expected, verifying no crash.
1067+ }
10531068}
1069+
1070+
You can’t perform that action at this time.
0 commit comments