Skip to content

Commit 09bc9ec

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 fa6c3df commit 09bc9ec

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,30 @@ 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 SpaceBeforeStmtStr.rtrim(AllowedCharacters).size() ==
164+
SpaceBeforeStmtStr.size();
165+
}
166+
154167
void fixGenericExprCastFromBool(DiagnosticBuilder &Diag,
155168
const ImplicitCastExpr *Cast,
156169
ASTContext &Context, StringRef OtherType) {
157170
const Expr *SubExpr = Cast->getSubExpr();
158-
bool NeedParens = !isa<ParenExpr>(SubExpr);
171+
const bool NeedParens = !isa<ParenExpr>(SubExpr->IgnoreImplicit());
172+
const bool NeedSpace = needsSpacePrefix(Cast->getBeginLoc(), Context);
159173

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

165179
if (NeedParens) {
166180
SourceLocation EndLoc = Lexer::getLocForEndOfToken(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,10 @@ Changes in existing checks
502502
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
503503
do-while loops into account for the `AllowIntegerConditions` and
504504
`AllowPointerConditions` options. It also now provides more consistent
505-
suggestions when parentheses are added to the return value or expressions.
506-
It also ignores false-positives for comparison containing bool bitfield.
505+
suggestions when parentheses are added to the return value or expressions.
506+
It also ignores false-positives for comparison containing ``bool`` bitfield.
507+
Auto-fix suggestions creating invalid code in certain scenarios have now been
508+
fixed.
507509

508510
- Improved :doc:`readability-misleading-indentation
509511
<clang-tidy/checks/readability/misleading-indentation>` check to ignore

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)