Skip to content

Commit

Permalink
[clang-tidy] Simplify throw keyword missing check
Browse files Browse the repository at this point in the history
Extend test to verify that it does not match in template instantiations.

Differential Revision: https://reviews.llvm.org/D96132
  • Loading branch information
steveire committed Feb 20, 2021
1 parent 6852a29 commit 77056fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
19 changes: 9 additions & 10 deletions clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ThrowKeywordMissingCheck : public ClangTidyCheck {
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
return TK_IgnoreUnlessSpelledInSource;
}
};

} // namespace bugprone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,17 @@ void nameContainsExceptionThrownTest(int i) {
template <class Exception>
void f(int i, Exception excToBeThrown) {}

template <class SomeType>
void templ(int i) {
if (i > 0)
SomeType();
}

void funcCallWithTempExcTest() {
f(5, RegularException());

templ<RegularException>(4);
templ<RegularClass>(4);
}

// Global variable initialization test.
Expand Down

0 comments on commit 77056fe

Please sign in to comment.