10#include "../utils/LexerUtils.h"
11#include "../utils/Matchers.h"
12#include "../utils/OptionsUtils.h"
13#include "clang/AST/ASTContext.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
18using clang::ast_matchers::internal::Matcher;
24AST_MATCHER_P(QualType, hasCleanType, Matcher<QualType>, InnerMatcher) {
25 return InnerMatcher.matches(
26 Node.getNonReferenceType().getUnqualifiedType().getCanonicalType(),
30constexpr std::array<StringRef, 2> NameList{
35Matcher<Expr> constructFrom(Matcher<QualType> TypeMatcher,
36 Matcher<Expr> ArgumentMatcher) {
40 cxxConstructExpr(argumentCountIs(1U), hasType(TypeMatcher),
41 hasArgument(0U, ArgumentMatcher)),
43 callExpr(argumentCountIs(1),
46 hasTemplateArgument(0, refersToType(TypeMatcher)))),
47 hasArgument(0, ArgumentMatcher))),
48 unless(anyOf(hasAncestor(typeLoc()),
49 hasAncestor(expr(matchers::hasUnevaluatedContext())))));
57 OptionalTypes(utils::options::parseStringList(
58 Options.get(
"OptionalTypes",
59 "::std::optional;::absl::optional;::boost::optional"))),
60 ValueMethods(utils::options::parseStringList(
61 Options.get(
"ValueMethods",
"::value$;::get$"))) {}
63std::optional<TraversalKind>
69 auto BindOptionalType = qualType(
70 hasCleanType(qualType(hasDeclaration(namedDecl(
72 .bind(
"optional-type")));
74 auto EqualsBoundOptionalType =
75 qualType(hasCleanType(equalsBoundNode(
"optional-type")));
77 auto OptionalDereferenceMatcher = callExpr(
79 cxxOperatorCallExpr(hasOverloadedOperatorName(
"*"),
80 hasUnaryOperand(hasType(EqualsBoundOptionalType)))
82 cxxMemberCallExpr(thisPointerType(EqualsBoundOptionalType),
83 callee(cxxMethodDecl(anyOf(
84 hasOverloadedOperatorName(
"*"),
86 .bind(
"member-call")),
87 hasType(qualType().bind(
"value-type")));
89 auto StdMoveCallMatcher =
90 callExpr(argumentCountIs(1), callee(functionDecl(hasName(
"::std::move"))),
91 hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
93 expr(constructFrom(BindOptionalType,
94 ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
95 StdMoveCallMatcher))))
109 const MatchFinder::MatchResult &Result) {
110 const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>(
"expr");
111 const auto *OptionalType = Result.Nodes.getNodeAs<QualType>(
"optional-type");
112 const auto *ValueType = Result.Nodes.getNodeAs<QualType>(
"value-type");
114 diag(MatchedExpr->getExprLoc(),
115 "conversion from %0 into %1 and back into %0, remove potentially "
116 "error-prone optional dereference")
117 << *OptionalType << ValueType->getUnqualifiedType();
119 if (
const auto *OperatorExpr =
120 Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"op-call")) {
121 diag(OperatorExpr->getExprLoc(),
"remove '*' to silence this warning",
123 << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
124 OperatorExpr->getBeginLoc(), OperatorExpr->getExprLoc()));
127 if (
const auto *CallExpr =
128 Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"member-call")) {
129 const SourceLocation Begin =
134 diag(CallExpr->getExprLoc(),
135 "remove call to %0 to silence this warning", DiagnosticIDs::Note);
136 Diag << CallExpr->getMethodDecl()
137 << FixItHint::CreateRemoval(
138 CharSourceRange::getTokenRange(Begin, CallExpr->getEndLoc()));
139 if (
const auto *Member =
140 llvm::dyn_cast<MemberExpr>(CallExpr->getCallee()->IgnoreImplicit());
141 Member && Member->isArrow())
142 Diag << FixItHint::CreateInsertion(CallExpr->getBeginLoc(),
"*");
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
OptionalValueConversionCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::optional< TraversalKind > getCheckTraversalKind() const override
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap