11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Lex/Lexer.h"
25static std::optional<Token>
27 const MatchFinder::MatchResult &Result) {
28 if (!Def->getReturnType().isLocalConstQualified())
35 const SourceLocation NameBeginLoc = Def->getQualifier()
36 ? Def->getQualifierLoc().getBeginLoc()
41 const CharSourceRange FileRange = Lexer::makeFileCharRange(
42 CharSourceRange::getCharRange(Def->getBeginLoc(), NameBeginLoc),
43 *Result.SourceManager, Result.Context->getLangOpts());
45 if (FileRange.isInvalid())
49 tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
54AST_MATCHER(QualType, isLocalConstQualified) {
55 return Node.isLocalConstQualified();
60 CharSourceRange ConstRange;
63 llvm::SmallVector<clang::FixItHint, 4> Hints;
66 llvm::SmallVector<clang::SourceLocation, 4> DeclLocs;
72static CheckResult
checkDef(
const clang::FunctionDecl *Def,
73 const MatchFinder::MatchResult &MatchResult) {
80 CharSourceRange::getCharRange(Tok->getLocation(), Tok->getEndLoc());
81 Result.Hints.push_back(FixItHint::CreateRemoval(Result.ConstRange));
86 for (
const FunctionDecl *Decl = Def->getPreviousDecl(); Decl !=
nullptr;
87 Decl = Decl->getPreviousDecl()) {
89 Result.Hints.push_back(FixItHint::CreateRemoval(
90 CharSourceRange::getCharRange(T->getLocation(), T->getEndLoc())));
93 Result.DeclLocs.push_back(Decl->getInnerLocStart());
99 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
106 functionDecl(returns(isLocalConstQualified()),
107 anyOf(isDefinition(), cxxMethodDecl(isPure())),
109 unless(cxxMethodDecl(isOverride())))
115 const auto *Def = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
118 (Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
121 const CheckResult CR =
checkDef(Def, Result);
126 const DiagnosticBuilder Diagnostic =
127 diag(Def->getInnerLocStart(),
128 "return type %0 is 'const'-qualified at the top level, which may "
129 "reduce code readability without improving const correctness")
130 << Def->getReturnType();
131 if (CR.ConstRange.isValid())
132 Diagnostic << CR.ConstRange;
135 const auto *Method = dyn_cast<CXXMethodDecl>(Def);
136 if (Method && Method->isVirtual())
139 for (
auto &Hint : CR.Hints)
142 for (
auto Loc : CR.DeclLocs)
143 diag(Loc,
"could not transform this declaration", DiagnosticIDs::Note);
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static CheckResult checkDef(const clang::FunctionDecl *Def, const MatchFinder::MatchResult &MatchResult)
static std::optional< Token > findConstToRemove(const ParmVarDecl &Param, const MatchFinder::MatchResult &Result)
std::optional< Token > getQualifyingToken(tok::TokenKind TK, CharSourceRange Range, const ASTContext &Context, const SourceManager &SM)
Assuming that Range spans a CVR-qualified type, returns the token in Range that is responsible for th...
llvm::StringMap< ClangTidyValue > OptionMap