diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h --- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h @@ -29,6 +29,9 @@ } void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + llvm::Optional getCheckTraversalKind() const override { + return TK_IgnoreUnlessSpelledInSource; + } }; } // namespace bugprone diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp @@ -21,17 +21,16 @@ cxxConstructorDecl(hasAnyConstructorInitializer(anything())); Finder->addMatcher( - expr(anyOf(cxxFunctionalCastExpr(), cxxBindTemporaryExpr(), - cxxTemporaryObjectExpr()), - hasType(cxxRecordDecl( - isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))), - unless(anyOf(hasAncestor(stmt( - anyOf(cxxThrowExpr(), callExpr(), returnStmt()))), - hasAncestor(varDecl()), - allOf(hasAncestor(CtorInitializerList), - unless(hasAncestor(cxxCatchStmt())))))) + cxxConstructExpr( + hasType(cxxRecordDecl( + isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))), + unless(anyOf(hasAncestor(stmt( + anyOf(cxxThrowExpr(), callExpr(), returnStmt()))), + hasAncestor(varDecl()), + allOf(hasAncestor(CtorInitializerList), + unless(hasAncestor(cxxCatchStmt())))))) .bind("temporary-exception-not-thrown"), - this); + this); } void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) { diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp @@ -94,8 +94,17 @@ template void f(int i, Exception excToBeThrown) {} +template +void templ(int i) { + if (i > 0) + SomeType(); +} + void funcCallWithTempExcTest() { f(5, RegularException()); + + templ(4); + templ(4); } // Global variable initialization test.