[clang-tidy] readability-container-size-empty fix for (*x).size()

llvm-svn: 298316
This commit is contained in:
Alexander Kornienko 2017-03-20 22:15:27 +00:00
parent 9bf7037ccd
commit f6cd367874
2 changed files with 6 additions and 3 deletions

View File

@ -56,8 +56,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher( Finder->addMatcher(
cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer), cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer),
hasType(pointsTo(ValidContainer)), hasType(pointsTo(ValidContainer)),
hasType(references(ValidContainer)))) hasType(references(ValidContainer))))),
.bind("STLObject")),
callee(cxxMethodDecl(hasName("size"))), WrongUse, callee(cxxMethodDecl(hasName("size"))), WrongUse,
unless(hasAncestor(cxxMethodDecl( unless(hasAncestor(cxxMethodDecl(
ofClass(equalsBoundNode("container")))))) ofClass(equalsBoundNode("container"))))))
@ -69,7 +68,7 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MemberCall = const auto *MemberCall =
Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr"); Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr");
const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp"); const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp");
const auto *E = Result.Nodes.getNodeAs<Expr>("STLObject"); const auto *E = MemberCall->getImplicitObjectArgument();
FixItHint Hint; FixItHint Hint;
std::string ReplacementText = std::string ReplacementText =
Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()), Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()),

View File

@ -168,6 +168,10 @@ int main() {
; ;
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
// CHECK-FIXES: {{^ }}if (vect3->empty()){{$}} // CHECK-FIXES: {{^ }}if (vect3->empty()){{$}}
if ((*vect3).size() == 0)
;
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
// CHECK-FIXES: {{^ }}if ((*vect3).empty()){{$}}
delete vect3; delete vect3;