[clang-tools-extra] 3496927 - [clang-tidy] Fixes to readability-implicit-bool-conversion (#72050)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 18 03:30:46 PST 2024
Author: Piotr Zegar
Date: 2024-02-18T12:30:43+01:00
New Revision: 3496927edcd0685807351ba88a7e2cfb006e1c0d
URL: https://github.com/llvm/llvm-project/commit/3496927edcd0685807351ba88a7e2cfb006e1c0d
DIFF: https://github.com/llvm/llvm-project/commit/3496927edcd0685807351ba88a7e2cfb006e1c0d.diff
LOG: [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
Added:
Modified:
clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 11706ffb5b7d4f..4f02950e7794cb 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 737ea9ba6d44f7..7ca7037e2a6a4f 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 6f1f2972971887..d6e7dcc4d8867b 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 );
+ }
+}
More information about the cfe-commits
mailing list