From 3496927edcd0685807351ba88a7e2cfb006e1c0d Mon Sep 17 00:00:00 2001 From: Piotr Zegar Date: Sun, 18 Feb 2024 12:30:43 +0100 Subject: [clang-tidy] Fixes to readability-implicit-bool-conversion (#72050) - Fixed issue with invalid code being generated when static_cast is put into fix, and no space were added before it. - Fixed issue with duplicate parentheses being added when double implicit cast is used. Closes #71848 --- .../readability/ImplicitBoolConversionCheck.cpp | 21 +++++++++++++++++---- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ .../readability/implicit-bool-conversion.cpp | 9 +++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) (limited to 'clang-tools-extra') diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 11706ff..4f02950 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 737ea9b..7ca7037 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 6f1f297..d6e7dcc 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 ); + } +} -- cgit v1.1