Skip to content

Commit 302cc84

Browse files
committed
[clang-tidy] Simplify boolean expr check
Differential Revision: https://reviews.llvm.org/D97153
1 parent b672870 commit 302cc84

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ const Expr *getBoolLiteral(const MatchFinder::MatchResult &Result,
7171
}
7272

7373
internal::BindableMatcher<Stmt> literalOrNegatedBool(bool Value) {
74-
return expr(anyOf(cxxBoolLiteral(equals(Value)),
75-
unaryOperator(hasUnaryOperand(ignoringParenImpCasts(
76-
cxxBoolLiteral(equals(!Value)))),
77-
hasOperatorName("!"))));
74+
return expr(
75+
anyOf(cxxBoolLiteral(equals(Value)),
76+
unaryOperator(hasUnaryOperand(cxxBoolLiteral(equals(!Value))),
77+
hasOperatorName("!"))));
7878
}
7979

8080
internal::Matcher<Stmt> returnsBool(bool Value, StringRef Id = "ignored") {
@@ -443,8 +443,7 @@ void SimplifyBooleanExprCheck::matchBoolCondition(MatchFinder *Finder,
443443
bool Value,
444444
StringRef BooleanId) {
445445
Finder->addMatcher(
446-
ifStmt(unless(isInTemplateInstantiation()),
447-
hasCondition(literalOrNegatedBool(Value).bind(BooleanId)))
446+
ifStmt(hasCondition(literalOrNegatedBool(Value).bind(BooleanId)))
448447
.bind(IfStmtId),
449448
this);
450449
}
@@ -453,8 +452,7 @@ void SimplifyBooleanExprCheck::matchTernaryResult(MatchFinder *Finder,
453452
bool Value,
454453
StringRef TernaryId) {
455454
Finder->addMatcher(
456-
conditionalOperator(unless(isInTemplateInstantiation()),
457-
hasTrueExpression(literalOrNegatedBool(Value)),
455+
conditionalOperator(hasTrueExpression(literalOrNegatedBool(Value)),
458456
hasFalseExpression(literalOrNegatedBool(!Value)))
459457
.bind(TernaryId),
460458
this);
@@ -463,14 +461,12 @@ void SimplifyBooleanExprCheck::matchTernaryResult(MatchFinder *Finder,
463461
void SimplifyBooleanExprCheck::matchIfReturnsBool(MatchFinder *Finder,
464462
bool Value, StringRef Id) {
465463
if (ChainedConditionalReturn)
466-
Finder->addMatcher(ifStmt(unless(isInTemplateInstantiation()),
467-
hasThen(returnsBool(Value, ThenLiteralId)),
464+
Finder->addMatcher(ifStmt(hasThen(returnsBool(Value, ThenLiteralId)),
468465
hasElse(returnsBool(!Value)))
469466
.bind(Id),
470467
this);
471468
else
472-
Finder->addMatcher(ifStmt(unless(isInTemplateInstantiation()),
473-
unless(hasParent(ifStmt())),
469+
Finder->addMatcher(ifStmt(unless(hasParent(ifStmt())),
474470
hasThen(returnsBool(Value, ThenLiteralId)),
475471
hasElse(returnsBool(!Value)))
476472
.bind(Id),
@@ -495,28 +491,22 @@ void SimplifyBooleanExprCheck::matchIfAssignsBool(MatchFinder *Finder,
495491
auto Else = anyOf(SimpleElse, compoundStmt(statementCountIs(1),
496492
hasAnySubstatement(SimpleElse)));
497493
if (ChainedConditionalAssignment)
498-
Finder->addMatcher(ifStmt(unless(isInTemplateInstantiation()),
499-
hasThen(Then), hasElse(Else))
500-
.bind(Id),
501-
this);
494+
Finder->addMatcher(ifStmt(hasThen(Then), hasElse(Else)).bind(Id), this);
502495
else
503-
Finder->addMatcher(ifStmt(unless(isInTemplateInstantiation()),
504-
unless(hasParent(ifStmt())), hasThen(Then),
505-
hasElse(Else))
506-
.bind(Id),
507-
this);
496+
Finder->addMatcher(
497+
ifStmt(unless(hasParent(ifStmt())), hasThen(Then), hasElse(Else))
498+
.bind(Id),
499+
this);
508500
}
509501

510502
void SimplifyBooleanExprCheck::matchCompoundIfReturnsBool(MatchFinder *Finder,
511503
bool Value,
512504
StringRef Id) {
513505
Finder->addMatcher(
514506
compoundStmt(
515-
unless(isInTemplateInstantiation()),
516507
hasAnySubstatement(
517508
ifStmt(hasThen(returnsBool(Value)), unless(hasElse(stmt())))),
518-
hasAnySubstatement(returnStmt(has(ignoringParenImpCasts(
519-
literalOrNegatedBool(!Value))))
509+
hasAnySubstatement(returnStmt(has(literalOrNegatedBool(!Value)))
520510
.bind(CompoundReturnId)))
521511
.bind(Id),
522512
this);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class SimplifyBooleanExprCheck : public ClangTidyCheck {
2727
void storeOptions(ClangTidyOptions::OptionMap &Options) override;
2828
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2929
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
30+
llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
31+
return TK_IgnoreUnlessSpelledInSource;
32+
}
3033

3134
private:
3235
class Visitor;

0 commit comments

Comments
 (0)