Skip to content

Commit 2602d88

Browse files
authored
[clang-tidy] Improved readability-bool-conversion to be more consistent when using parentheses (#72068)
Provides more consistent suggestions when parentheses are added to the return value. Fixes #71852
1 parent 2aec866 commit 2602d88

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression,
152152
return "false";
153153
}
154154

155-
if (const auto *IntLit = dyn_cast<IntegerLiteral>(Expression)) {
155+
if (const auto *IntLit =
156+
dyn_cast<IntegerLiteral>(Expression->IgnoreParens())) {
156157
return (IntLit->getValue() == 0) ? "false" : "true";
157158
}
158159

@@ -385,7 +386,7 @@ void ImplicitBoolConversionCheck::handleCastFromBool(
385386
<< DestType;
386387

387388
if (const auto *BoolLiteral =
388-
dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr())) {
389+
dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr()->IgnoreParens())) {
389390
Diag << tooling::fixit::createReplacement(
390391
*Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context));
391392
} else {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ Changes in existing checks
416416
- Improved :doc:`readability-implicit-bool-conversion
417417
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
418418
do-while loops into account for the `AllowIntegerConditions` and
419-
`AllowPointerConditions` options.
419+
`AllowPointerConditions` options. It also now provides more consistent
420+
suggestions when parentheses are added to the return value.
420421

421422
- Improved :doc:`readability-non-const-parameter
422423
<clang-tidy/checks/readability/non-const-parameter>` check to ignore

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,36 @@ bool f(S& s) {
472472

473473
} // namespace ignore_1bit_bitfields
474474

475+
int implicitConversionReturnInt()
476+
{
477+
return true;
478+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion bool -> 'int'
479+
// CHECK-FIXES: return 1
480+
}
481+
482+
int implicitConversionReturnIntWithParens()
483+
{
484+
return (true);
485+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion bool -> 'int'
486+
// CHECK-FIXES: return 1
487+
}
488+
489+
490+
bool implicitConversionReturnBool()
491+
{
492+
return 1;
493+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion 'int' -> bool
494+
// CHECK-FIXES: return true
495+
}
496+
497+
bool implicitConversionReturnBoolWithParens()
498+
{
499+
return (1);
500+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion 'int' -> bool
501+
// CHECK-FIXES: return true
502+
}
503+
504+
475505
namespace PR47000 {
476506
int to_int(bool x) { return int{x}; }
477507

0 commit comments

Comments
 (0)