-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy] Fixes to readability-implicit-bool-conversion #72050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PiotrZSL
merged 1 commit into
llvm:main
from
PiotrZSL:71848-clang-tidy-readability-implicit-bool-conversion-suggests-invalid-fix
Feb 18, 2024
Merged
[clang-tidy] Fixes to readability-implicit-bool-conversion #72050
PiotrZSL
merged 1 commit into
llvm:main
from
PiotrZSL:71848-clang-tidy-readability-implicit-bool-conversion-suggests-invalid-fix
Feb 18, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Piotr Zegar (PiotrZSL) Changes
Closes #71848 Full diff: https://github.com/llvm/llvm-project/pull/72050.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 69e6d73c4fcd7bb..5e71b2fc81de7f0 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -173,16 +173,30 @@ 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 SpaceBeforeStmtStr.rtrim(AllowedCharacters).size() ==
+ SpaceBeforeStmtStr.size();
+}
+
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 f49c412118e7d98..e0353e4344e9b6a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -410,7 +410,8 @@ Changes in existing checks
- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
do-while loops into account for the `AllowIntegerConditions` and
- `AllowPointerConditions` options.
+ `AllowPointerConditions` options. Additionally, an issue with auto-fix
+ suggestions generating invalid code in certain scenarios has been resolved.
- Improved :doc:`readability-non-const-parameter
<clang-tidy/checks/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 323cf813c047000..3929984204905d3 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
@@ -478,3 +478,12 @@ namespace PR47000 {
using IntType = int;
int to_int2(bool x) { return IntType{x}; }
}
+
+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 );
+ }
+}
|
HerrCai0907
reviewed
Jan 16, 2024
clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
Outdated
Show resolved
Hide resolved
cf2f1ae
to
f8a3b89
Compare
HerrCai0907
approved these changes
Feb 18, 2024
- 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.
2b4452d
to
f77e3fc
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #71848