[clang-tidy] Simplify throw keyword missing check

Extend test to verify that it does not match in template instantiations.

Differential Revision: https://reviews.llvm.org/D96132
This commit is contained in:
Stephen Kelly 2020-12-28 01:54:04 +00:00
parent 6852a29a3b
commit 77056fe58e
3 changed files with 21 additions and 10 deletions

View File

@ -21,17 +21,16 @@ void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
cxxConstructorDecl(hasAnyConstructorInitializer(anything())); cxxConstructorDecl(hasAnyConstructorInitializer(anything()));
Finder->addMatcher( Finder->addMatcher(
expr(anyOf(cxxFunctionalCastExpr(), cxxBindTemporaryExpr(), cxxConstructExpr(
cxxTemporaryObjectExpr()), hasType(cxxRecordDecl(
hasType(cxxRecordDecl( isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))),
isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))), unless(anyOf(hasAncestor(stmt(
unless(anyOf(hasAncestor(stmt( anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
anyOf(cxxThrowExpr(), callExpr(), returnStmt()))), hasAncestor(varDecl()),
hasAncestor(varDecl()), allOf(hasAncestor(CtorInitializerList),
allOf(hasAncestor(CtorInitializerList), unless(hasAncestor(cxxCatchStmt()))))))
unless(hasAncestor(cxxCatchStmt()))))))
.bind("temporary-exception-not-thrown"), .bind("temporary-exception-not-thrown"),
this); this);
} }
void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) { void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) {

View File

@ -29,6 +29,9 @@ public:
} }
void registerMatchers(ast_matchers::MatchFinder *Finder) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
return TK_IgnoreUnlessSpelledInSource;
}
}; };
} // namespace bugprone } // namespace bugprone

View File

@ -94,8 +94,17 @@ void nameContainsExceptionThrownTest(int i) {
template <class Exception> template <class Exception>
void f(int i, Exception excToBeThrown) {} void f(int i, Exception excToBeThrown) {}
template <class SomeType>
void templ(int i) {
if (i > 0)
SomeType();
}
void funcCallWithTempExcTest() { void funcCallWithTempExcTest() {
f(5, RegularException()); f(5, RegularException());
templ<RegularException>(4);
templ<RegularClass>(4);
} }
// Global variable initialization test. // Global variable initialization test.