10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Lex/Lexer.h"
14#include "clang/Tooling/FixIt.h"
21 auto SemanticallyDifferentContainer = allOf(
25 cxxConstructorDecl(parameterCountIs(3),
26 hasParameter(0, hasType(qualType(hasCanonicalType(
28 hasType(cxxRecordDecl(hasAnyName(
"::std::basic_string",
"::std::vector",
29 "::std::deque",
"::std::forward_list",
36 hasDeclaration(cxxConstructorDecl(isExplicit())),
39 isListInitialization(), hasDescendant(initListExpr()),
41 SemanticallyDifferentContainer)))
45 returnStmt(hasReturnValue(ConstructExpr),
46 forFunction(functionDecl(returns(unless(anyOf(builtinType(),
53 const auto *MatchedFunctionDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"fn");
54 const auto *MatchedConstructExpr =
55 Result.Nodes.getNodeAs<CXXConstructExpr>(
"ctor");
58 SourceLocation
Loc = MatchedConstructExpr->getExprLoc();
64 MatchedFunctionDecl->getReturnType().getCanonicalType();
65 const QualType ConstructType =
66 MatchedConstructExpr->getType().getCanonicalType();
70 auto Diag =
diag(
Loc,
"avoid repeating the return type from the "
71 "declaration; use a braced initializer list instead");
73 const SourceRange CallParensRange =
74 MatchedConstructExpr->getParenOrBraceRange();
77 if (CallParensRange.isInvalid())
81 for (
unsigned I = 0, NumParams = MatchedConstructExpr->getNumArgs();
83 if (
const auto *VD = dyn_cast<VarDecl>(
84 MatchedConstructExpr->getConstructor()->getParamDecl(I))) {
85 if (MatchedConstructExpr->getArg(I)->getType().getCanonicalType() !=
86 VD->getType().getCanonicalType())
92 CharSourceRange CtorCallSourceRange = CharSourceRange::getTokenRange(
93 Loc, CallParensRange.getBegin().getLocWithOffset(-1));
95 Diag << FixItHint::CreateRemoval(CtorCallSourceRange)
96 << FixItHint::CreateReplacement(CallParensRange.getBegin(),
"{")
97 << 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.