1 //===--- ReturnBracedInitListCheck.cpp - clang-tidy------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ReturnBracedInitListCheck.h"
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"
16 using namespace clang::ast_matchers
;
18 namespace clang::tidy::modernize
{
20 void ReturnBracedInitListCheck::registerMatchers(MatchFinder
*Finder
) {
21 auto SemanticallyDifferentContainer
= allOf(
23 // Container(size_type count, const T &value,
24 // const Allocator &alloc = Allocator());
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",
35 // Skip explicit constructor.
36 hasDeclaration(cxxConstructorDecl(isExplicit())),
37 // Skip list initialization and constructors with an initializer
39 isListInitialization(), hasDescendant(initListExpr()),
40 // Skip container `vector(size_type, const T&, ...)`.
41 SemanticallyDifferentContainer
)))
45 returnStmt(hasReturnValue(ConstructExpr
),
46 forFunction(functionDecl(returns(unless(anyOf(builtinType(),
52 void ReturnBracedInitListCheck::check(const MatchFinder::MatchResult
&Result
) {
53 const auto *MatchedFunctionDecl
= Result
.Nodes
.getNodeAs
<FunctionDecl
>("fn");
54 const auto *MatchedConstructExpr
=
55 Result
.Nodes
.getNodeAs
<CXXConstructExpr
>("ctor");
57 // Don't make replacements in macro.
58 SourceLocation Loc
= MatchedConstructExpr
->getExprLoc();
62 // Make sure that the return type matches the constructed type.
63 const QualType ReturnType
=
64 MatchedFunctionDecl
->getReturnType().getCanonicalType();
65 const QualType ConstructType
=
66 MatchedConstructExpr
->getType().getCanonicalType();
67 if (ReturnType
!= ConstructType
)
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();
76 // Make sure there is an explicit constructor call.
77 if (CallParensRange
.isInvalid())
80 // Make sure that the ctor arguments match the declaration.
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())
91 // Range for constructor name and opening brace.
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(), "}");
100 } // namespace clang::tidy::modernize