[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / modernize / ReturnBracedInitListCheck.cpp
blobd7796666d0db25210a69e4a3b32c5295c40a7b6c
1 //===--- ReturnBracedInitListCheck.cpp - clang-tidy------------------------===//
2 //
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
6 //
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(
22 hasDeclaration(
23 // Container(size_type count, const T &value,
24 // const Allocator &alloc = Allocator());
25 cxxConstructorDecl(parameterCountIs(3),
26 hasParameter(0, hasType(qualType(hasCanonicalType(
27 isInteger())))))),
28 hasType(cxxRecordDecl(hasAnyName("::std::basic_string", "::std::vector",
29 "::std::deque", "::std::forward_list",
30 "::std::list"))));
32 auto ConstructExpr =
33 cxxConstructExpr(
34 unless(anyOf(
35 // Skip explicit constructor.
36 hasDeclaration(cxxConstructorDecl(isExplicit())),
37 // Skip list initialization and constructors with an initializer
38 // list.
39 isListInitialization(), hasDescendant(initListExpr()),
40 // Skip container `vector(size_type, const T&, ...)`.
41 SemanticallyDifferentContainer)))
42 .bind("ctor");
44 Finder->addMatcher(
45 returnStmt(hasReturnValue(ConstructExpr),
46 forFunction(functionDecl(returns(unless(anyOf(builtinType(),
47 autoType()))))
48 .bind("fn"))),
49 this);
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();
59 if (Loc.isMacroID())
60 return;
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)
68 return;
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())
78 return;
80 // Make sure that the ctor arguments match the declaration.
81 for (unsigned I = 0, NumParams = MatchedConstructExpr->getNumArgs();
82 I < NumParams; ++I) {
83 if (const auto *VD = dyn_cast<VarDecl>(
84 MatchedConstructExpr->getConstructor()->getParamDecl(I))) {
85 if (MatchedConstructExpr->getArg(I)->getType().getCanonicalType() !=
86 VD->getType().getCanonicalType())
87 return;
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