Skip to content

Commit 4d29460

Browse files
authored
[clang-tidy] Correctly add parentheses in readability-implicit-bool-conversion (llvm#162215)
For `CompoundAssignOperator` in condition, there will be two layers of `ImplicitCastExpr`, for code: ``` int val = -1; while(val >>= 7) { } ``` While statement's AST: ``` WhileStmt <line:4:5, line:5:5> |-ImplicitCastExpr <line:4:11, col:18> 'bool' <IntegralToBoolean> | `-ImplicitCastExpr <col:11, col:18> 'int' <LValueToRValue> | `-CompoundAssignOperator <col:11, col:18> 'int' lvalue '>>=' ComputeLHSTy='int' ComputeResultTy='int' | |-DeclRefExpr <col:11> 'int' lvalue Var 0x20290cb8 'val' 'int' | `-IntegerLiteral <col:18> 'int' 7 `-CompoundStmt <col:21, line:5:5> ``` This is not taken into account by the check when determining whether brackets need to be added. Closes llvm#161318.
1 parent a5e30f8 commit 4d29460

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ static void fixGenericExprCastToBool(DiagnosticBuilder &Diag,
8989

9090
const Expr *SubExpr = Cast->getSubExpr();
9191

92-
bool NeedInnerParens = utils::fixit::areParensNeededForStatement(*SubExpr);
92+
bool NeedInnerParens =
93+
utils::fixit::areParensNeededForStatement(*SubExpr->IgnoreImpCasts());
9394
bool NeedOuterParens =
9495
Parent != nullptr && utils::fixit::areParensNeededForStatement(*Parent);
9596

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ Changes in existing checks
402402
declarations and macros in system headers. The documentation is also improved
403403
to differentiate the general options from the specific ones.
404404

405+
- Improved :doc:`readability-implicit-bool-conversion
406+
<clang-tidy/checks/readability/implicit-bool-conversion>` check by correctly
407+
adding parentheses when the inner expression are implicitly converted
408+
multiple times.
409+
405410
- Improved :doc:`readability-qualified-auto
406411
<clang-tidy/checks/readability/qualified-auto>` check by adding the option
407412
`IgnoreAliasing`, that allows not looking at underlying types of type aliases.

clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,13 @@ namespace PR71848 {
547547
// CHECK-FIXES: return static_cast<int>( foo );
548548
}
549549
}
550+
551+
namespace PR161318 {
552+
int AddParenOutsideOfCompoundAssignOp() {
553+
int val = -1;
554+
while(val >>= 7) {
555+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: implicit conversion 'int' -> 'bool' [readability-implicit-bool-conversion]
556+
// CHECK-FIXES: while((val >>= 7) != 0) {
557+
}
558+
}
559+
}

0 commit comments

Comments
 (0)