diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 69e6d73c4fcd7..f0fca30de3b3c 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -152,7 +152,8 @@ StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression, return "false"; } - if (const auto *IntLit = dyn_cast(Expression)) { + if (const auto *IntLit = + dyn_cast(Expression->IgnoreParens())) { return (IntLit->getValue() == 0) ? "false" : "true"; } @@ -385,7 +386,7 @@ void ImplicitBoolConversionCheck::handleCastFromBool( << DestType; if (const auto *BoolLiteral = - dyn_cast(Cast->getSubExpr())) { + dyn_cast(Cast->getSubExpr()->IgnoreParens())) { Diag << tooling::fixit::createReplacement( *Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context)); } else { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index caf4569670720..353c6fe202692 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -416,7 +416,8 @@ Changes in existing checks - Improved :doc:`readability-implicit-bool-conversion ` check to take do-while loops into account for the `AllowIntegerConditions` and - `AllowPointerConditions` options. + `AllowPointerConditions` options. It also now provides more consistent + suggestions when parentheses are added to the return value. - Improved :doc:`readability-non-const-parameter ` check to ignore diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp index 323cf813c0470..f7f5d506a9ce0 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp @@ -472,6 +472,36 @@ bool f(S& s) { } // namespace ignore_1bit_bitfields +int implicitConversionReturnInt() +{ + return true; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion bool -> 'int' + // CHECK-FIXES: return 1 +} + +int implicitConversionReturnIntWithParens() +{ + return (true); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion bool -> 'int' + // CHECK-FIXES: return 1 +} + + +bool implicitConversionReturnBool() +{ + return 1; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion 'int' -> bool + // CHECK-FIXES: return true +} + +bool implicitConversionReturnBoolWithParens() +{ + return (1); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion 'int' -> bool + // CHECK-FIXES: return true +} + + namespace PR47000 { int to_int(bool x) { return int{x}; }