1 //===--- UnusedRaiiCheck.cpp - clang-tidy ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "UnusedRaiiCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/Lex/Lexer.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang {
16 namespace tidy {
17 namespace bugprone {
18
19 namespace {
AST_MATCHER(CXXRecordDecl,hasNonTrivialDestructor)20 AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
21 // TODO: If the dtor is there but empty we don't want to warn either.
22 return Node.hasDefinition() && Node.hasNonTrivialDestructor();
23 }
24 } // namespace
25
registerMatchers(MatchFinder * Finder)26 void UnusedRaiiCheck::registerMatchers(MatchFinder *Finder) {
27 // Look for temporaries that are constructed in-place and immediately
28 // destroyed. Look for temporaries created by a functional cast but not for
29 // those returned from a call.
30 auto BindTemp = cxxBindTemporaryExpr(
31 unless(has(ignoringParenImpCasts(callExpr()))),
32 unless(has(ignoringParenImpCasts(objcMessageExpr()))))
33 .bind("temp");
34 Finder->addMatcher(
35 traverse(ast_type_traits::TK_AsIs,
36 exprWithCleanups(
37 unless(isInTemplateInstantiation()),
38 hasParent(compoundStmt().bind("compound")),
39 hasType(cxxRecordDecl(hasNonTrivialDestructor())),
40 anyOf(has(ignoringParenImpCasts(BindTemp)),
41 has(ignoringParenImpCasts(cxxFunctionalCastExpr(
42 has(ignoringParenImpCasts(BindTemp)))))))
43 .bind("expr")),
44 this);
45 }
46
check(const MatchFinder::MatchResult & Result)47 void UnusedRaiiCheck::check(const MatchFinder::MatchResult &Result) {
48 const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
49
50 // We ignore code expanded from macros to reduce the number of false
51 // positives.
52 if (E->getBeginLoc().isMacroID())
53 return;
54
55 // Don't emit a warning for the last statement in the surrounding compound
56 // statement.
57 const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>("compound");
58 if (E == CS->body_back())
59 return;
60
61 // Emit a warning.
62 auto D = diag(E->getBeginLoc(), "object destroyed immediately after "
63 "creation; did you mean to name the object?");
64 const char *Replacement = " give_me_a_name";
65
66 // If this is a default ctor we have to remove the parens or we'll introduce a
67 // most vexing parse.
68 const auto *BTE = Result.Nodes.getNodeAs<CXXBindTemporaryExpr>("temp");
69 if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
70 if (TOE->getNumArgs() == 0) {
71 D << FixItHint::CreateReplacement(
72 CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
73 Replacement);
74 return;
75 }
76
77 // Otherwise just suggest adding a name. To find the place to insert the name
78 // find the first TypeLoc in the children of E, which always points to the
79 // written type.
80 auto Matches =
81 match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
82 if (const auto *TL = selectFirst<TypeLoc>("t", Matches))
83 D << FixItHint::CreateInsertion(
84 Lexer::getLocForEndOfToken(TL->getEndLoc(), 0, *Result.SourceManager,
85 getLangOpts()),
86 Replacement);
87 }
88
89 } // namespace bugprone
90 } // namespace tidy
91 } // namespace clang
92