[clang-tidy] Treat all types with non-trivial destructors as RAII.

This solves some false negatives at a cost of adding some false positives that
can be fixed easily and (almost) automatically.

llvm-svn: 237120
This commit is contained in:
Alexander Kornienko 2015-05-12 12:17:20 +00:00
parent c07f848785
commit bdaa681fc6
2 changed files with 11 additions and 3 deletions

View File

@ -15,9 +15,9 @@ using namespace clang::ast_matchers;
namespace clang {
namespace ast_matchers {
AST_MATCHER(CXXRecordDecl, hasUserDeclaredDestructor) {
AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
// TODO: If the dtor is there but empty we don't want to warn either.
return Node.hasDefinition() && Node.hasUserDeclaredDestructor();
return Node.hasDefinition() && Node.hasNonTrivialDestructor();
}
} // namespace ast_matchers
@ -32,7 +32,7 @@ void UnusedRAIICheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
exprWithCleanups(unless(isInTemplateInstantiation()),
hasParent(compoundStmt().bind("compound")),
hasType(recordDecl(hasUserDeclaredDestructor())),
hasType(recordDecl(hasNonTrivialDestructor())),
anyOf(has(BindTemp), has(functionalCastExpr(
has(BindTemp))))).bind("expr"),
this);

View File

@ -10,6 +10,10 @@ struct Foo {
struct Bar {
Bar();
};
struct FooBar {
FooBar();
Foo f;
};
@ -47,6 +51,10 @@ void test() {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
// CHECK-FIXES: TFoo<int> give_me_a_name(23);
FooBar();
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
// CHECK-FIXES: FooBar give_me_a_name;
Bar();
f();
qux<Foo>();