11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
22struct NotExtendedByDeclBoundToPredicate {
23 bool operator()(
const internal::BoundNodesMap &Nodes)
const {
24 const auto *Other = Nodes.getNodeAs<ValueDecl>(
ID);
28 const auto *Self =
Node.get<MaterializeTemporaryExpr>();
32 return Self->getExtendingDecl() != Other;
36 ::clang::DynTypedNode
Node;
39AST_MATCHER_P(MaterializeTemporaryExpr, isExtendedByDeclBoundTo, StringRef,
41 NotExtendedByDeclBoundToPredicate Predicate{
42 ID, ::clang::DynTypedNode::create(
Node)};
43 return Builder->removeBindings(Predicate);
49 const LangOptions &LangOpts)
const {
50 return LangOpts.CPlusPlus;
53std::optional<TraversalKind>
59 MatchFinder *Finder) {
61 varDecl(unless(isExpansionInSystemHeader()),
62 hasType(qualType(references(qualType().bind(
"type")))),
64 hasInitializer(expr(hasDescendant(
65 materializeTemporaryExpr(
66 isExtendedByDeclBoundTo(
"var"),
67 has(expr(anyOf(cxxTemporaryObjectExpr(), initListExpr(),
69 hasType(qualType(equalsBoundNode(
"type"))))))
70 .bind(
"temporary"))))),
75 const MatchFinder::MatchResult &Result) {
76 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(
"var");
77 const auto *MatchedTemporary = Result.Nodes.getNodeAs<Expr>(
"temporary");
79 diag(MatchedDecl->getLocation(),
80 "reference variable %0 extends the lifetime of a just-constructed "
81 "temporary object %1, consider changing reference to value")
82 << MatchedDecl << MatchedTemporary->getType();
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::optional< TraversalKind > getCheckTraversalKind() const override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)