Skip to content

Commit

Permalink
[clang-tidy] Fixes to readability-implicit-bool-conversion (#72050)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
PiotrZSL committed Feb 18, 2024
1 parent 1e4c76c commit 3496927
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit 3496927

Please sign in to comment.