diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 11706ffb5b7d4..4f02950e7794c 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -151,16 +151,29 @@ StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression, return {}; } +bool needsSpacePrefix(SourceLocation Loc, ASTContext &Context) { + SourceRange PrefixRange(Loc.getLocWithOffset(-1), Loc); + StringRef SpaceBeforeStmtStr = Lexer::getSourceText( + CharSourceRange::getCharRange(PrefixRange), Context.getSourceManager(), + Context.getLangOpts(), nullptr); + if (SpaceBeforeStmtStr.empty()) + return true; + + const StringRef AllowedCharacters(" \t\n\v\f\r(){}[]<>;,+=-|&~!^*/"); + return !AllowedCharacters.contains(SpaceBeforeStmtStr.back()); +} + void fixGenericExprCastFromBool(DiagnosticBuilder &Diag, const ImplicitCastExpr *Cast, ASTContext &Context, StringRef OtherType) { const Expr *SubExpr = Cast->getSubExpr(); - bool NeedParens = !isa(SubExpr); + const bool NeedParens = !isa(SubExpr->IgnoreImplicit()); + const bool NeedSpace = needsSpacePrefix(Cast->getBeginLoc(), Context); Diag << FixItHint::CreateInsertion( - Cast->getBeginLoc(), - (Twine("static_cast<") + OtherType + ">" + (NeedParens ? "(" : "")) - .str()); + Cast->getBeginLoc(), (Twine() + (NeedSpace ? " " : "") + "static_cast<" + + OtherType + ">" + (NeedParens ? "(" : "")) + .str()); if (NeedParens) { SourceLocation EndLoc = Lexer::getLocForEndOfToken( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 737ea9ba6d44f..7ca7037e2a6a4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -174,6 +174,11 @@ Changes in existing checks ` check to also remove any trailing whitespace when deleting the ``virtual`` keyword. +- Improved :doc:`readability-implicit-bool-conversion + ` check to provide + valid fix suggestions for ``static_cast`` without a preceding space and + fixed problem with duplicate parentheses in double implicit casts. + - Improved :doc:`readability-redundant-inline-specifier ` check to properly emit warnings for static data member with an in-class initializer. 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 6f1f297297188..d6e7dcc4d8867 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 @@ -524,3 +524,12 @@ namespace PR71867 { // CHECK-FIXES: return (x ? 1 : 0) != 0; } } + +namespace PR71848 { + int fun() { + bool foo = false; + return( foo ); +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: implicit conversion 'bool' -> 'int' [readability-implicit-bool-conversion] +// CHECK-FIXES: return static_cast( foo ); + } +}