diff options
author | Piotr Zegar <me@piotrzegar.pl> | 2024-02-18 12:30:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-18 12:30:43 +0100 |
commit | 3496927edcd0685807351ba88a7e2cfb006e1c0d (patch) | |
tree | 2dbc8271428870b1a210a5ca763e4ab0d1ceafc8 | |
parent | 1e4c76cdc9d47c06eb30c811fd497d7b0a3ae5e8 (diff) |
[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
3 files changed, 31 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 11706ffb5b7d..4f02950e7794 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<ParenExpr>(SubExpr); + const bool NeedParens = !isa<ParenExpr>(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 737ea9ba6d44..7ca7037e2a6a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -174,6 +174,11 @@ Changes in existing checks <clang-tidy/checks/modernize/use-override>` check to also remove any trailing whitespace when deleting the ``virtual`` keyword. +- Improved :doc:`readability-implicit-bool-conversion + <clang-tidy/checks/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 <clang-tidy/checks/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 6f1f29729718..d6e7dcc4d886 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<int>( foo ); + } +} |