Skip to content

Commit f77e3fc

Browse files
committed
[clang-tidy] Fixes to readability-implicit-bool-conversion
- 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.
1 parent 833fea4 commit f77e3fc

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,29 @@ StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression,
151151
return {};
152152
}
153153

154+
bool needsSpacePrefix(SourceLocation Loc, ASTContext &Context) {
155+
SourceRange PrefixRange(Loc.getLocWithOffset(-1), Loc);
156+
StringRef SpaceBeforeStmtStr = Lexer::getSourceText(
157+
CharSourceRange::getCharRange(PrefixRange), Context.getSourceManager(),
158+
Context.getLangOpts(), nullptr);
159+
if (SpaceBeforeStmtStr.empty())
160+
return true;
161+
162+
const StringRef AllowedCharacters(" \t\n\v\f\r(){}[]<>;,+=-|&~!^*/");
163+
return !AllowedCharacters.contains(SpaceBeforeStmtStr.back());
164+
}
165+
154166
void fixGenericExprCastFromBool(DiagnosticBuilder &Diag,
155167
const ImplicitCastExpr *Cast,
156168
ASTContext &Context, StringRef OtherType) {
157169
const Expr *SubExpr = Cast->getSubExpr();
158-
bool NeedParens = !isa<ParenExpr>(SubExpr);
170+
const bool NeedParens = !isa<ParenExpr>(SubExpr->IgnoreImplicit());
171+
const bool NeedSpace = needsSpacePrefix(Cast->getBeginLoc(), Context);
159172

160173
Diag << FixItHint::CreateInsertion(
161-
Cast->getBeginLoc(),
162-
(Twine("static_cast<") + OtherType + ">" + (NeedParens ? "(" : ""))
163-
.str());
174+
Cast->getBeginLoc(), (Twine() + (NeedSpace ? " " : "") + "static_cast<" +
175+
OtherType + ">" + (NeedParens ? "(" : ""))
176+
.str());
164177

165178
if (NeedParens) {
166179
SourceLocation EndLoc = Lexer::getLocForEndOfToken(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ Changes in existing checks
174174
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
175175
whitespace when deleting the ``virtual`` keyword.
176176

177+
- Improved :doc:`readability-implicit-bool-conversion
178+
<clang-tidy/checks/readability/implicit-bool-conversion>` check to provide
179+
valid fix suggestions for ``static_cast`` without a preceding space and
180+
fixed problem with duplicate parentheses in double implicit casts.
181+
177182
- Improved :doc:`readability-redundant-inline-specifier
178183
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
179184
emit warnings for static data member with an in-class initializer.

clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,12 @@ namespace PR71867 {
524524
// CHECK-FIXES: return (x ? 1 : 0) != 0;
525525
}
526526
}
527+
528+
namespace PR71848 {
529+
int fun() {
530+
bool foo = false;
531+
return( foo );
532+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: implicit conversion 'bool' -> 'int' [readability-implicit-bool-conversion]
533+
// CHECK-FIXES: return static_cast<int>( foo );
534+
}
535+
}

0 commit comments

Comments
 (0)