diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -422,7 +422,7 @@ bool Value, StringRef BooleanId) { Finder->addMatcher( - ifStmt(isExpansionInMainFile(), + ifStmt(isExpansionInMainFile(), unless(isInTemplateInstantiation()), hasCondition(cxxBoolLiteral(equals(Value)).bind(BooleanId))) .bind(IfStmtId), this); @@ -433,6 +433,7 @@ StringRef TernaryId) { Finder->addMatcher( conditionalOperator(isExpansionInMainFile(), + unless(isInTemplateInstantiation()), hasTrueExpression(cxxBoolLiteral(equals(Value))), hasFalseExpression(cxxBoolLiteral(equals(!Value)))) .bind(TernaryId), @@ -443,12 +444,14 @@ bool Value, StringRef Id) { if (ChainedConditionalReturn) Finder->addMatcher(ifStmt(isExpansionInMainFile(), + unless(isInTemplateInstantiation()), hasThen(returnsBool(Value, ThenLiteralId)), hasElse(returnsBool(!Value))) .bind(Id), this); else Finder->addMatcher(ifStmt(isExpansionInMainFile(), + unless(isInTemplateInstantiation()), unless(hasParent(ifStmt())), hasThen(returnsBool(Value, ThenLiteralId)), hasElse(returnsBool(!Value))) @@ -474,12 +477,16 @@ auto Else = anyOf(SimpleElse, compoundStmt(statementCountIs(1), hasAnySubstatement(SimpleElse))); if (ChainedConditionalAssignment) - Finder->addMatcher(ifStmt(hasThen(Then), hasElse(Else)).bind(Id), this); + Finder->addMatcher(ifStmt(unless(isInTemplateInstantiation()), + hasThen(Then), hasElse(Else)) + .bind(Id), + this); else - Finder->addMatcher( - ifStmt(unless(hasParent(ifStmt())), hasThen(Then), hasElse(Else)) - .bind(Id), - this); + Finder->addMatcher(ifStmt(unless(isInTemplateInstantiation()), + unless(hasParent(ifStmt())), hasThen(Then), + hasElse(Else)) + .bind(Id), + this); } void SimplifyBooleanExprCheck::matchCompoundIfReturnsBool(MatchFinder *Finder, @@ -487,6 +494,7 @@ StringRef Id) { Finder->addMatcher( compoundStmt( + unless(isInTemplateInstantiation()), hasAnySubstatement( ifStmt(hasThen(returnsBool(Value)), unless(hasElse(stmt())))), hasAnySubstatement(returnStmt(has(ignoringParenImpCasts( diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp @@ -948,3 +948,18 @@ } // CHECK-MESSAGES: :[[@LINE-4]]:12: warning: {{.*}} in conditional return // CHECK-FIXES: S == (A)S;{{$}} + +template +void ignoreInstantiations() { + if (B) { + return; + } else { + return; + } +} + +void instantiate() { + // Just make sure the check isn't fooled by template instantiations. + ignoreInstantiations(); + ignoreInstantiations(); +}