| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ReturnBracedInitListCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/Lex/Lexer.h" |
| 13 | #include "clang/Tooling/FixIt.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang::tidy::modernize { |
| 18 | |
| 19 | void ReturnBracedInitListCheck::registerMatchers(MatchFinder *Finder) { |
| 20 | // Skip list initialization and constructors with an initializer list. |
| 21 | auto ConstructExpr = |
| 22 | cxxConstructExpr( |
| 23 | unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())), |
| 24 | isListInitialization(), hasDescendant(initListExpr())))) |
| 25 | .bind("ctor"); |
| 26 | |
| 27 | Finder->addMatcher( |
| 28 | returnStmt(hasReturnValue(ConstructExpr), |
| 29 | forFunction(functionDecl(returns(unless(anyOf(builtinType(), |
| 30 | autoType())))) |
| 31 | .bind("fn"))), |
| 32 | this); |
| 33 | } |
| 34 | |
| 35 | void ReturnBracedInitListCheck::check(const MatchFinder::MatchResult &Result) { |
| 36 | const auto *MatchedFunctionDecl = Result.Nodes.getNodeAs<FunctionDecl>("fn"); |
| 37 | const auto *MatchedConstructExpr = |
| 38 | Result.Nodes.getNodeAs<CXXConstructExpr>("ctor"); |
| 39 | |
| 40 | // Don't make replacements in macro. |
| 41 | SourceLocation Loc = MatchedConstructExpr->getExprLoc(); |
| 42 | if (Loc.isMacroID()) |
| 43 | return; |
| 44 | |
| 45 | // Make sure that the return type matches the constructed type. |
| 46 | const QualType ReturnType = |
| 47 | MatchedFunctionDecl->getReturnType().getCanonicalType(); |
| 48 | const QualType ConstructType = |
| 49 | MatchedConstructExpr->getType().getCanonicalType(); |
| 50 | if (ReturnType != ConstructType) |
| 51 | return; |
| 52 | |
| 53 | auto Diag = diag(Loc, "avoid repeating the return type from the " |
| 54 | "declaration; use a braced initializer list instead"); |
| 55 | |
| 56 | const SourceRange CallParensRange = |
| 57 | MatchedConstructExpr->getParenOrBraceRange(); |
| 58 | |
| 59 | // Make sure there is an explicit constructor call. |
| 60 | if (CallParensRange.isInvalid()) |
| 61 | return; |
| 62 | |
| 63 | // Make sure that the ctor arguments match the declaration. |
| 64 | for (unsigned I = 0, NumParams = MatchedConstructExpr->getNumArgs(); |
| 65 | I < NumParams; ++I) { |
| 66 | if (const auto *VD = dyn_cast<VarDecl>( |
| 67 | MatchedConstructExpr->getConstructor()->getParamDecl(I))) { |
| 68 | if (MatchedConstructExpr->getArg(I)->getType().getCanonicalType() != |
| 69 | VD->getType().getCanonicalType()) |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Range for constructor name and opening brace. |
| 75 | CharSourceRange CtorCallSourceRange = CharSourceRange::getTokenRange( |
| 76 | Loc, CallParensRange.getBegin().getLocWithOffset(-1)); |
| 77 | |
| 78 | Diag << FixItHint::CreateRemoval(CtorCallSourceRange) |
| 79 | << FixItHint::CreateReplacement(CallParensRange.getBegin(), "{") |
| 80 | << FixItHint::CreateReplacement(CallParensRange.getEnd(), "}"); |
| 81 | } |
| 82 | |
| 83 | } // namespace clang::tidy::modernize |