Skip to content

Commit cf2f1ae

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 d4360e4 commit cf2f1ae

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,30 @@ StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression,
173173
return {};
174174
}
175175

176+
bool needsSpacePrefix(SourceLocation Loc, ASTContext &Context) {
177+
SourceRange PrefixRange(Loc.getLocWithOffset(-1), Loc);
178+
StringRef SpaceBeforeStmtStr = Lexer::getSourceText(
179+
CharSourceRange::getCharRange(PrefixRange), Context.getSourceManager(),
180+
Context.getLangOpts(), nullptr);
181+
if (SpaceBeforeStmtStr.empty())
182+
return true;
183+
184+
const StringRef AllowedCharacters(" \t\n\v\f\r(){}[]<>;,+=-|&~!^*/");
185+
return SpaceBeforeStmtStr.rtrim(AllowedCharacters).size() ==
186+
SpaceBeforeStmtStr.size();
187+
}
188+
176189
void fixGenericExprCastFromBool(DiagnosticBuilder &Diag,
177190
const ImplicitCastExpr *Cast,
178191
ASTContext &Context, StringRef OtherType) {
179192
const Expr *SubExpr = Cast->getSubExpr();
180-
bool NeedParens = !isa<ParenExpr>(SubExpr);
193+
const bool NeedParens = !isa<ParenExpr>(SubExpr->IgnoreImplicit());
194+
const bool NeedSpace = needsSpacePrefix(Cast->getBeginLoc(), Context);
181195

182196
Diag << FixItHint::CreateInsertion(
183-
Cast->getBeginLoc(),
184-
(Twine("static_cast<") + OtherType + ">" + (NeedParens ? "(" : ""))
185-
.str());
197+
Cast->getBeginLoc(), (Twine() + (NeedSpace ? " " : "") + "static_cast<" +
198+
OtherType + ">" + (NeedParens ? "(" : ""))
199+
.str());
186200

187201
if (NeedParens) {
188202
SourceLocation EndLoc = Lexer::getLocForEndOfToken(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ Changes in existing checks
410410
- Improved :doc:`readability-implicit-bool-conversion
411411
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
412412
do-while loops into account for the `AllowIntegerConditions` and
413-
`AllowPointerConditions` options.
413+
`AllowPointerConditions` options. Additionally, an issue with auto-fix
414+
suggestions generating invalid code in certain scenarios has been resolved.
414415

415416
- Improved :doc:`readability-non-const-parameter
416417
<clang-tidy/checks/readability/non-const-parameter>` 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
@@ -478,3 +478,12 @@ namespace PR47000 {
478478
using IntType = int;
479479
int to_int2(bool x) { return IntType{x}; }
480480
}
481+
482+
namespace PR71848 {
483+
int fun() {
484+
bool foo = false;
485+
return( foo );
486+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: implicit conversion bool -> 'int' [readability-implicit-bool-conversion]
487+
// CHECK-FIXES: return static_cast<int>( foo );
488+
}
489+
}

0 commit comments

Comments
 (0)