10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13#include "clang/Tooling/FixIt.h"
23 unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
24 isListInitialization(), hasDescendant(initListExpr()))))
28 returnStmt(hasReturnValue(ConstructExpr),
29 forFunction(functionDecl(returns(unless(anyOf(builtinType(),
36 const auto *MatchedFunctionDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"fn");
37 const auto *MatchedConstructExpr =
38 Result.Nodes.getNodeAs<CXXConstructExpr>(
"ctor");
41 SourceLocation
Loc = MatchedConstructExpr->getExprLoc();
47 MatchedFunctionDecl->getReturnType().getCanonicalType();
48 const QualType ConstructType =
49 MatchedConstructExpr->getType().getCanonicalType();
53 auto Diag =
diag(
Loc,
"avoid repeating the return type from the "
54 "declaration; use a braced initializer list instead");
56 const SourceRange CallParensRange =
57 MatchedConstructExpr->getParenOrBraceRange();
60 if (CallParensRange.isInvalid())
64 for (
unsigned I = 0, NumParams = MatchedConstructExpr->getNumArgs();
66 if (
const auto *VD = dyn_cast<VarDecl>(
67 MatchedConstructExpr->getConstructor()->getParamDecl(I))) {
68 if (MatchedConstructExpr->getArg(I)->getType().getCanonicalType() !=
69 VD->getType().getCanonicalType())
75 CharSourceRange CtorCallSourceRange = CharSourceRange::getTokenRange(
76 Loc, CallParensRange.getBegin().getLocWithOffset(-1));
78 Diag << FixItHint::CreateRemoval(CtorCallSourceRange)
79 << FixItHint::CreateReplacement(CallParensRange.getBegin(),
"{")
80 << FixItHint::CreateReplacement(CallParensRange.getEnd(),
"}");
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.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.