Skip to content

Commit 537d4e9

Browse files
committed
Revert "Added options to readability-implicit-bool-conversion (#120087)"
This reverts commit 5bec2b7. (llvmorg-20-init-16425-g5bec2b71b44d) This broke tests.
1 parent 5bec2b7 commit 537d4e9

File tree

5 files changed

+68
-183
lines changed

5 files changed

+68
-183
lines changed

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

Lines changed: 64 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,13 @@ ImplicitBoolConversionCheck::ImplicitBoolConversionCheck(
259259
AllowIntegerConditions(Options.get("AllowIntegerConditions", false)),
260260
AllowPointerConditions(Options.get("AllowPointerConditions", false)),
261261
UseUpperCaseLiteralSuffix(
262-
Options.get("UseUpperCaseLiteralSuffix", false)),
263-
CheckConversionsToBool(Options.get("CheckConversionsToBool", true)),
264-
CheckConversionsFromBool(Options.get("CheckConversionsFromBool", true)) {}
262+
Options.get("UseUpperCaseLiteralSuffix", false)) {}
265263

266264
void ImplicitBoolConversionCheck::storeOptions(
267265
ClangTidyOptions::OptionMap &Opts) {
268266
Options.store(Opts, "AllowIntegerConditions", AllowIntegerConditions);
269267
Options.store(Opts, "AllowPointerConditions", AllowPointerConditions);
270268
Options.store(Opts, "UseUpperCaseLiteralSuffix", UseUpperCaseLiteralSuffix);
271-
Options.store(Opts, "CheckConversionsToBool", CheckConversionsToBool);
272-
Options.store(Opts, "CheckConversionsFromBool", CheckConversionsFromBool);
273269
}
274270

275271
void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
@@ -281,7 +277,6 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
281277
expr(hasType(qualType().bind("type")),
282278
hasParent(initListExpr(hasParent(explicitCastExpr(
283279
hasType(qualType(equalsBoundNode("type"))))))))));
284-
285280
auto ImplicitCastFromBool = implicitCastExpr(
286281
anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
287282
// Prior to C++11 cast from bool literal to pointer was allowed.
@@ -292,84 +287,72 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
292287
auto BoolXor =
293288
binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
294289
hasRHS(ImplicitCastFromBool));
290+
auto ComparisonInCall = allOf(
291+
hasParent(callExpr()),
292+
hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
293+
295294
auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
296295
isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
297296

298-
if (CheckConversionsToBool) {
299-
auto ComparisonInCall = allOf(
300-
hasParent(callExpr()),
301-
hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
302-
303-
Finder->addMatcher(
304-
traverse(
305-
TK_AsIs,
306-
implicitCastExpr(
307-
anyOf(hasCastKind(CK_IntegralToBoolean),
308-
hasCastKind(CK_FloatingToBoolean),
309-
hasCastKind(CK_PointerToBoolean),
310-
hasCastKind(CK_MemberPointerToBoolean)),
311-
// Exclude cases of C23 comparison result.
312-
unless(allOf(isC23(),
313-
hasSourceExpression(ignoringParens(
314-
binaryOperator(hasAnyOperatorName(
315-
">", ">=", "==", "!=", "<", "<=")))))),
316-
// Exclude case of using if or while statements with variable
317-
// declaration, e.g.:
318-
// if (int var = functionCall()) {}
319-
unless(hasParent(
320-
stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))),
321-
// Exclude cases common to implicit cast to and from bool.
322-
unless(ExceptionCases), unless(has(BoolXor)),
323-
// Exclude C23 cases common to implicit cast to bool.
324-
unless(ComparisonInCall),
325-
// Retrieve also parent statement, to check if we need
326-
// additional parens in replacement.
327-
optionally(hasParent(stmt().bind("parentStmt"))),
328-
unless(isInTemplateInstantiation()),
329-
unless(IsInCompilerGeneratedFunction))
330-
.bind("implicitCastToBool")),
331-
this);
332-
}
333-
334-
if (CheckConversionsFromBool) {
335-
336-
auto BoolComparison = binaryOperator(hasAnyOperatorName("==", "!="),
337-
hasLHS(ImplicitCastFromBool),
338-
hasRHS(ImplicitCastFromBool));
339-
340-
auto BoolOpAssignment = binaryOperator(
341-
hasAnyOperatorName("|=", "&="), hasLHS(expr(hasType(booleanType()))));
342-
343-
auto BitfieldAssignment = binaryOperator(
344-
hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
345-
346-
auto BitfieldConstruct =
347-
cxxConstructorDecl(hasDescendant(cxxCtorInitializer(
348-
withInitializer(equalsBoundNode("implicitCastFromBool")),
349-
forField(hasBitWidth(1)))));
350-
351-
Finder->addMatcher(
352-
traverse(
353-
TK_AsIs,
354-
implicitCastExpr(
355-
ImplicitCastFromBool, unless(ExceptionCases),
356-
// Exclude comparisons of bools, as they are
357-
// always cast to integers in such context:
358-
// bool_expr_a == bool_expr_b
359-
// bool_expr_a != bool_expr_b
360-
unless(hasParent(binaryOperator(anyOf(BoolComparison, BoolXor,
361-
BoolOpAssignment,
362-
BitfieldAssignment)))),
363-
implicitCastExpr().bind("implicitCastFromBool"),
364-
unless(hasParent(BitfieldConstruct)),
365-
// Check also for nested casts, for example:
366-
// bool -> int -> float.
367-
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
368-
anything()),
369-
unless(isInTemplateInstantiation()),
370-
unless(IsInCompilerGeneratedFunction))),
371-
this);
372-
}
297+
Finder->addMatcher(
298+
traverse(TK_AsIs,
299+
implicitCastExpr(
300+
anyOf(hasCastKind(CK_IntegralToBoolean),
301+
hasCastKind(CK_FloatingToBoolean),
302+
hasCastKind(CK_PointerToBoolean),
303+
hasCastKind(CK_MemberPointerToBoolean)),
304+
// Exclude cases of C23 comparison result.
305+
unless(allOf(isC23(),
306+
hasSourceExpression(ignoringParens(
307+
binaryOperator(hasAnyOperatorName(
308+
">", ">=", "==", "!=", "<", "<=")))))),
309+
// Exclude case of using if or while statements with variable
310+
// declaration, e.g.:
311+
// if (int var = functionCall()) {}
312+
unless(hasParent(
313+
stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))),
314+
// Exclude cases common to implicit cast to and from bool.
315+
unless(ExceptionCases), unless(has(BoolXor)),
316+
// Exclude C23 cases common to implicit cast to bool.
317+
unless(ComparisonInCall),
318+
// Retrieve also parent statement, to check if we need
319+
// additional parens in replacement.
320+
optionally(hasParent(stmt().bind("parentStmt"))),
321+
unless(isInTemplateInstantiation()),
322+
unless(IsInCompilerGeneratedFunction))
323+
.bind("implicitCastToBool")),
324+
this);
325+
326+
auto BoolComparison = binaryOperator(hasAnyOperatorName("==", "!="),
327+
hasLHS(ImplicitCastFromBool),
328+
hasRHS(ImplicitCastFromBool));
329+
auto BoolOpAssignment = binaryOperator(hasAnyOperatorName("|=", "&="),
330+
hasLHS(expr(hasType(booleanType()))));
331+
auto BitfieldAssignment = binaryOperator(
332+
hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
333+
auto BitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer(
334+
withInitializer(equalsBoundNode("implicitCastFromBool")),
335+
forField(hasBitWidth(1)))));
336+
Finder->addMatcher(
337+
traverse(
338+
TK_AsIs,
339+
implicitCastExpr(
340+
ImplicitCastFromBool, unless(ExceptionCases),
341+
// Exclude comparisons of bools, as they are always cast to
342+
// integers in such context:
343+
// bool_expr_a == bool_expr_b
344+
// bool_expr_a != bool_expr_b
345+
unless(hasParent(
346+
binaryOperator(anyOf(BoolComparison, BoolXor,
347+
BoolOpAssignment, BitfieldAssignment)))),
348+
implicitCastExpr().bind("implicitCastFromBool"),
349+
unless(hasParent(BitfieldConstruct)),
350+
// Check also for nested casts, for example: bool -> int -> float.
351+
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
352+
anything()),
353+
unless(isInTemplateInstantiation()),
354+
unless(IsInCompilerGeneratedFunction))),
355+
this);
373356
}
374357

375358
void ImplicitBoolConversionCheck::check(

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class ImplicitBoolConversionCheck : public ClangTidyCheck {
3737
const bool AllowIntegerConditions;
3838
const bool AllowPointerConditions;
3939
const bool UseUpperCaseLiteralSuffix;
40-
const bool CheckConversionsToBool;
41-
const bool CheckConversionsFromBool;
4240
};
4341

4442
} // namespace clang::tidy::readability

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,10 @@ Changes in existing checks
342342
diagnostic.
343343

344344
- Improved :doc:`readability-implicit-bool-conversion
345-
<clang-tidy/checks/readability/implicit-bool-conversion>` check by adding the
346-
option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in
347-
fixes and fixing false positive for implicit conversion of comparison result in
348-
C23, and by adding the option `CheckConversionsToBool` or
349-
`CheckConversionsFromBool` to configure checks for conversions involving ``bool``.
345+
<clang-tidy/checks/readability/implicit-bool-conversion>` check
346+
by adding the option `UseUpperCaseLiteralSuffix` to select the
347+
case of the literal suffix in fixes and fixing false positive for implicit
348+
conversion of comparison result in C23.
350349

351350
- Improved :doc:`readability-redundant-smartptr-get
352351
<clang-tidy/checks/readability/redundant-smartptr-get>` check to

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -147,41 +147,3 @@ Options
147147
if (foo) {}
148148
// ^ propose replacement default: if (foo != 0u) {}
149149
// ^ propose replacement with option `UseUpperCaseLiteralSuffix`: if (foo != 0U) {}
150-
151-
.. option:: CheckConversionsToBool
152-
153-
When `true`, the check diagnoses implicit conversions to ``bool``.
154-
Default is `true`.
155-
156-
Example
157-
158-
.. code-block:: c++
159-
160-
int x = 42;
161-
if (x) {}
162-
// ^ propose replacement: if (x != 0) {}
163-
164-
float f = 3.14;
165-
if (f) {}
166-
// ^ propose replacement: if (f != 0.0f) {}
167-
168-
.. option:: CheckConversionsFromBool
169-
170-
When `true`, the check diagnoses implicit conversions from ``bool``.
171-
Default is `true`.
172-
173-
Example
174-
175-
.. code-block:: c++
176-
177-
bool b = true;
178-
179-
int x = b;
180-
// ^ propose replacement: int x = b ? 1 : 0;
181-
182-
float f = b;
183-
// ^ propose replacement: float f = b ? 1.0f : 0.0f;
184-
185-
int* p = b;
186-
// ^ propose replacement: int* p = b ? some_ptr : nullptr;
187-

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)