77 const auto NonMemberMatcher = expr(ignoringImplicit(ignoringParenImpCasts(
79 hasParent(stmt(optionally(hasParent(stmtExpr().bind(
"stexpr"))))
81 unless(hasAncestor(classTemplateDecl())),
82 callee(functionDecl(hasName(
"empty"), unless(returns(voidType())))))
84 const auto MemberMatcher =
85 expr(ignoringImplicit(ignoringParenImpCasts(cxxMemberCallExpr(
86 hasParent(stmt(optionally(hasParent(stmtExpr().bind(
"stexpr"))))
88 callee(cxxMethodDecl(hasName(
"empty"),
89 unless(returns(voidType()))))))))
92 Finder->addMatcher(MemberMatcher,
this);
93 Finder->addMatcher(NonMemberMatcher,
this);
98 if (Result.Nodes.getNodeAs<Expr>(
"parent"))
101 const auto *PParentStmtExpr = Result.Nodes.getNodeAs<Expr>(
"stexpr");
102 const auto *ParentCompStmt = Result.Nodes.getNodeAs<CompoundStmt>(
"parent");
103 const auto *ParentCond =
getCondition(Result.Nodes,
"parent");
104 const auto *ParentReturnStmt = Result.Nodes.getNodeAs<ReturnStmt>(
"parent");
106 if (
const auto *MemberCall =
107 Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"empty")) {
109 if (ParentCond == MemberCall->getExprStmt())
113 if (PParentStmtExpr && ParentCompStmt &&
114 ParentCompStmt->body_back() == MemberCall->getExprStmt())
117 if (ParentReturnStmt)
120 const SourceLocation MemberLoc = MemberCall->getBeginLoc();
121 const SourceLocation ReplacementLoc = MemberCall->getExprLoc();
122 const SourceRange ReplacementRange =
123 SourceRange(ReplacementLoc, ReplacementLoc);
125 ASTContext &Context = MemberCall->getRecordDecl()->getASTContext();
126 const DeclarationName Name =
127 Context.DeclarationNames.getIdentifier(&Context.Idents.get(
"clear"));
129 auto Candidates = HeuristicResolver(Context).lookupDependentName(
130 MemberCall->getRecordDecl(), Name, [](
const NamedDecl *ND) {
131 return isa<CXXMethodDecl>(ND) &&
132 cast<CXXMethodDecl>(ND)->getMinRequiredArguments() == 0 &&
133 !cast<CXXMethodDecl>(ND)->isConst();
136 const bool HasClear = !Candidates.empty();
138 const auto *Clear = cast<CXXMethodDecl>(Candidates.at(0));
139 const QualType RangeType =
140 MemberCall->getImplicitObjectArgument()->getType();
141 const bool QualifierIncompatible =
142 (!Clear->isVolatile() && RangeType.isVolatileQualified()) ||
143 RangeType.isConstQualified();
144 if (!QualifierIncompatible) {
146 "ignoring the result of 'empty()'; did you mean 'clear()'? ")
147 << FixItHint::CreateReplacement(ReplacementRange,
"clear");
152 diag(MemberLoc,
"ignoring the result of 'empty()'");
154 }
else if (
const auto *NonMemberCall =
155 Result.Nodes.getNodeAs<CallExpr>(
"empty")) {
156 if (ParentCond == NonMemberCall->getExprStmt())
158 if (PParentStmtExpr && ParentCompStmt &&
159 ParentCompStmt->body_back() == NonMemberCall->getExprStmt())
161 if (ParentReturnStmt)
163 if (NonMemberCall->getNumArgs() != 1)
166 const SourceLocation NonMemberLoc = NonMemberCall->getExprLoc();
167 const SourceLocation NonMemberEndLoc = NonMemberCall->getEndLoc();
169 const Expr *Arg = NonMemberCall->getArg(0);
170 CXXRecordDecl *ArgRecordDecl = Arg->getType()->getAsCXXRecordDecl();
171 if (ArgRecordDecl ==
nullptr)
174 ASTContext &Context = ArgRecordDecl->getASTContext();
175 const DeclarationName Name =
176 Context.DeclarationNames.getIdentifier(&Context.Idents.get(
"clear"));
178 auto Candidates = HeuristicResolver(Context).lookupDependentName(
179 ArgRecordDecl, Name, [](
const NamedDecl *ND) {
180 return isa<CXXMethodDecl>(ND) &&
181 cast<CXXMethodDecl>(ND)->getMinRequiredArguments() == 0 &&
182 !cast<CXXMethodDecl>(ND)->isConst();
185 const bool HasClear = !Candidates.empty();
188 const auto *Clear = cast<CXXMethodDecl>(Candidates.at(0));
189 const bool QualifierIncompatible =
190 (!Clear->isVolatile() && Arg->getType().isVolatileQualified()) ||
191 Arg->getType().isConstQualified();
192 if (!QualifierIncompatible) {
193 const std::string ReplacementText =
194 std::string(Lexer::getSourceText(
195 CharSourceRange::getTokenRange(Arg->getSourceRange()),
196 *Result.SourceManager, getLangOpts())) +
198 const SourceRange ReplacementRange =
199 SourceRange(NonMemberLoc, NonMemberEndLoc);
201 "ignoring the result of '%0'; did you mean 'clear()'?")
202 << dyn_cast<NamedDecl>(NonMemberCall->getCalleeDecl())
203 ->getQualifiedNameAsString()
204 << FixItHint::CreateReplacement(ReplacementRange, ReplacementText);
209 diag(NonMemberLoc,
"ignoring the result of '%0'")
210 << dyn_cast<NamedDecl>(NonMemberCall->getCalleeDecl())
211 ->getQualifiedNameAsString();