Skip to content

Commit 729403e

Browse files
authored
[clang-tidy] Correcting issues in readability-implicit-bool-conversion on C23 (#92241)
`readability-implicit-bool-conversion` supports language-versions with `LangOpts.Bool` which includes C23. This PR corrects an issue that the fixer suggests `static_cast<>()` which is not available in C23, and will instead suggest C-style casts on other than C++. The fixer will also suggest using `nullptr` instead of `0` and avoid a problem with recursive fixes on C23. The recursive issue, a function taking bool and a comparison, is now excluded as in C++.
1 parent 558cb29 commit 729403e

File tree

4 files changed

+374
-4
lines changed

4 files changed

+374
-4
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind,
5050

5151
case CK_PointerToBoolean:
5252
case CK_MemberPointerToBoolean: // Fall-through on purpose.
53-
return Context.getLangOpts().CPlusPlus11 ? "nullptr" : "0";
53+
return (Context.getLangOpts().CPlusPlus11 || Context.getLangOpts().C23)
54+
? "nullptr"
55+
: "0";
5456

5557
default:
5658
llvm_unreachable("Unexpected cast kind");
@@ -165,6 +167,12 @@ bool needsSpacePrefix(SourceLocation Loc, ASTContext &Context) {
165167
void fixGenericExprCastFromBool(DiagnosticBuilder &Diag,
166168
const ImplicitCastExpr *Cast,
167169
ASTContext &Context, StringRef OtherType) {
170+
if (!Context.getLangOpts().CPlusPlus) {
171+
Diag << FixItHint::CreateInsertion(Cast->getBeginLoc(),
172+
(Twine("(") + OtherType + ")").str());
173+
return;
174+
}
175+
168176
const Expr *SubExpr = Cast->getSubExpr();
169177
const bool NeedParens = !isa<ParenExpr>(SubExpr->IgnoreImplicit());
170178
const bool NeedSpace = needsSpacePrefix(Cast->getBeginLoc(), Context);
@@ -267,6 +275,10 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
267275
auto BoolXor =
268276
binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
269277
hasRHS(ImplicitCastFromBool));
278+
auto ComparisonInCall = allOf(
279+
hasParent(callExpr()),
280+
hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
281+
270282
Finder->addMatcher(
271283
traverse(TK_AsIs,
272284
implicitCastExpr(
@@ -281,6 +293,8 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
281293
stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))),
282294
// Exclude cases common to implicit cast to and from bool.
283295
unless(ExceptionCases), unless(has(BoolXor)),
296+
// Exclude C23 cases common to implicit cast to bool.
297+
unless(ComparisonInCall),
284298
// Retrieve also parent statement, to check if we need
285299
// additional parens in replacement.
286300
optionally(hasParent(stmt().bind("parentStmt"))),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ Changes in existing checks
381381
- Improved :doc:`readability-implicit-bool-conversion
382382
<clang-tidy/checks/readability/implicit-bool-conversion>` check to provide
383383
valid fix suggestions for ``static_cast`` without a preceding space and
384-
fixed problem with duplicate parentheses in double implicit casts.
384+
fixed problem with duplicate parentheses in double implicit casts. Corrected
385+
the fix suggestions for C23 and later by using C-style casts instead of
386+
``static_cast``.
385387

386388
- Improved :doc:`readability-redundant-inline-specifier
387389
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly

clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ The rules for generating fix-it hints are:
9696
- ``if (!pointer)`` is changed to ``if (pointer == nullptr)``,
9797

9898
- in case of conversions from bool to other built-in types, an explicit
99-
``static_cast`` is proposed to make it clear that a conversion is taking
100-
place:
99+
``static_cast`` (or a C-style cast since C23) is proposed to make it clear
100+
that a conversion is taking place:
101101

102102
- ``int integer = boolean;`` is changed to
103103
``int integer = static_cast<int>(boolean);``,

0 commit comments

Comments
 (0)