20 anyOf(cxxMemberCallExpr(
21 on(expr(anyOf(hasType(OnClass),
22 hasType(qualType(pointsTo(
23 decl(OnClass).bind(
"ptr_to_ptr"))))))
24 .bind(
"smart_pointer")),
26 memberExpr(hasObjectExpression(cxxThisExpr())))),
27 callee(cxxMethodDecl(hasName(
"get"),
28 returns(qualType(pointsTo(
29 type().bind(
"getType"))))))),
30 cxxDependentScopeMemberExpr(
33 expr(hasType(qualType(hasCanonicalType(
34 templateSpecializationType(hasDeclaration(
35 classTemplateDecl(has(cxxRecordDecl(
37 hasMethod(cxxMethodDecl(
41 "getType")))))))))))))))
42 .bind(
"smart_pointer")))))
43 .bind(
"redundant_get");
52 MatchFinder::MatchCallback *Callback) {
53 const auto MatchesOpArrow =
54 allOf(hasName(
"operator->"),
55 returns(qualType(pointsTo(type().bind(
"op->Type")))));
56 const auto MatchesOpStar =
57 allOf(hasName(
"operator*"),
58 returns(qualType(references(type().bind(
"op*Type")))));
59 const auto HasRelevantOps =
60 allOf(anyOf(hasMethod(MatchesOpArrow),
61 has(functionTemplateDecl(has(functionDecl(MatchesOpArrow))))),
62 anyOf(hasMethod(MatchesOpStar),
63 has(functionTemplateDecl(has(functionDecl(MatchesOpStar))))));
65 const auto QuacksLikeASmartptr =
66 cxxRecordDecl(cxxRecordDecl().bind(
"duck_typing"), HasRelevantOps);
69 const auto SmartptrAny = anyOf(
knownSmartptr(), QuacksLikeASmartptr);
70 const auto SmartptrWithDeref = anyOf(
71 cxxRecordDecl(
knownSmartptr(), HasRelevantOps), QuacksLikeASmartptr);
75 memberExpr(expr().bind(
"memberExpr"), isArrow(),
76 hasObjectExpression(
callToGet(SmartptrWithDeref))),
81 unaryOperator(hasOperatorName(
"*"),
82 hasUnaryOperand(
callToGet(SmartptrWithDeref))),
87 recordDecl(SmartptrAny, has(cxxConversionDecl(returns(booleanType())))));
89 unaryOperator(hasOperatorName(
"!"), hasUnaryOperand(CallToGetAsBool)),
93 Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback);
96 Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)),
99 Finder->addMatcher(cxxDependentScopeMemberExpr(hasObjectExpression(
133 if (Result.Nodes.getNodeAs<Decl>(
"duck_typing") ==
nullptr)
139 const Type *OpArrowType =
140 Result.Nodes.getNodeAs<Type>(
"op->Type")->getUnqualifiedDesugaredType();
141 const Type *OpStarType =
142 Result.Nodes.getNodeAs<Type>(
"op*Type")->getUnqualifiedDesugaredType();
143 const Type *GetType =
144 Result.Nodes.getNodeAs<Type>(
"getType")->getUnqualifiedDesugaredType();
145 return OpArrowType == OpStarType && OpArrowType == GetType;
152 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>(
"ptr_to_ptr") !=
nullptr;
153 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>(
"memberExpr") !=
nullptr;
154 const auto *GetCall = Result.Nodes.getNodeAs<Expr>(
"redundant_get");
155 if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
158 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>(
"smart_pointer");
160 if (IsPtrToPtr && IsMemberExpr) {
165 auto SR = GetCall->getSourceRange();
168 if (isa<CXXDependentScopeMemberExpr>(GetCall))
169 SR.setEnd(Lexer::getLocForEndOfToken(SR.getEnd(), 0, *Result.SourceManager,
171 .getLocWithOffset(1));
173 StringRef SmartptrText = Lexer::getSourceText(
174 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
175 *Result.SourceManager, getLangOpts());
177 if (SmartptrText.ends_with(
"->")) {
178 SmartptrText = SmartptrText.drop_back(2);
181 std::string Replacement = Twine(IsPtrToPtr ?
"*" :
"", SmartptrText).str();
182 diag(GetCall->getBeginLoc(),
"redundant get() call on smart pointer")
183 << FixItHint::CreateReplacement(SR, Replacement);