33const DeclaratorDecl *getConstructedVarOrField(
const Expr *FoundConstructExpr,
61 auto FindConstructor =
63 parameterCountIs(1), isExplicit())
65 auto FindConstructExpr =
67 hasDeclaration(FindConstructor), argumentCountIs(1),
70 hasType(hasCanonicalType(pointerType(
73 .bind(ConstructExprN);
74 Finder->addMatcher(FindConstructExpr,
this);
78 const auto *FoundNewExpr = Result.Nodes.getNodeAs<CXXNewExpr>(NewExprN);
79 const auto *FoundConstructExpr =
80 Result.Nodes.getNodeAs<CXXConstructExpr>(ConstructExprN);
81 const auto *FoundConstructorDecl =
82 Result.Nodes.getNodeAs<CXXConstructorDecl>(ConstructorN);
84 ASTContext &Ctx = FoundConstructorDecl->getASTContext();
85 const DeclaratorDecl *VarOrField =
86 getConstructedVarOrField(FoundConstructExpr, Ctx);
88 auto D = diag(FoundNewExpr->getBeginLoc(),
89 "%0 pointer to non-array is initialized with array")
91 D << FoundNewExpr->getSourceRange();
94 auto TSTypeLoc = VarOrField->getTypeSourceInfo()
96 .getAsAdjusted<clang::TemplateSpecializationTypeLoc>();
97 assert(TSTypeLoc.getNumArgs() >= 1 &&
98 "Matched type should have at least 1 template argument.");
100 SourceRange TemplateArgumentRange = TSTypeLoc.getArgLoc(0)
104 D << TemplateArgumentRange;
106 if (isInSingleDeclStmt(VarOrField)) {
107 const SourceManager &SM = Ctx.getSourceManager();
111 SourceLocation InsertLoc = Lexer::getLocForEndOfToken(
112 TemplateArgumentRange.getEnd(), 0, SM, Ctx.getLangOpts());
113 D << FixItHint::CreateInsertion(InsertLoc,
"[]");
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.