Index: clang-tidy/readability/ContainerSizeEmptyCheck.cpp =================================================================== --- clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -37,7 +37,7 @@ namedDecl( has(cxxMethodDecl( isConst(), parameterCountIs(0), isPublic(), - hasName("size"), + anyOf(hasName("size"), hasName("length")), returns(qualType(isInteger(), unless(booleanType())))) .bind("size")), has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(), @@ -62,7 +62,7 @@ cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer), hasType(pointsTo(ValidContainer)), hasType(references(ValidContainer))))), - callee(cxxMethodDecl(hasName("size"))), WrongUse, + callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))), WrongUse, unless(hasAncestor(cxxMethodDecl( ofClass(equalsBoundNode("container")))))) .bind("SizeCallExpr"), @@ -203,7 +203,8 @@ if (MemberCall) { diag(MemberCall->getBeginLoc(), "the 'empty' method should be used to check " - "for emptiness instead of 'size'") + "for emptiness instead of '%0'") + << MemberCall->getMethodDecl()->getName() << Hint; } else { diag(BinCmp->getBeginLoc(), Index: test/clang-tidy/readability-container-size-empty.cpp =================================================================== --- test/clang-tidy/readability-container-size-empty.cpp +++ test/clang-tidy/readability-container-size-empty.cpp @@ -17,6 +17,7 @@ bool operator!=(const char *) const; basic_string operator+(const basic_string& other) const; unsigned long size() const; + unsigned long length() const; bool empty() const; }; @@ -106,30 +107,42 @@ std::string str2; std::wstring wstr; (void)(str.size() + 0); + (void)(str.length() + 0); (void)(str.size() - 0); + (void)(str.length() - 0); (void)(0 + str.size()); + (void)(0 + str.length()); (void)(0 - str.size()); + (void)(0 - str.length()); if (intSet.size() == 0) ; // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] // CHECK-FIXES: {{^ }}if (intSet.empty()){{$}} - // CHECK-MESSAGES: :32:8: note: method 'set'::empty() defined here + // CHECK-MESSAGES: :33:8: note: method 'set'::empty() defined here if (intSet == std::set()) ; // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness // CHECK-FIXES: {{^ }}if (intSet.empty()){{$}} - // CHECK-MESSAGES: :32:8: note: method 'set'::empty() defined here + // CHECK-MESSAGES: :33:8: note: method 'set'::empty() defined here if (s_func() == "") ; // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used // CHECK-FIXES: {{^ }}if (s_func().empty()){{$}} if (str.size() == 0) ; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if (str.empty()){{$}} + if (str.length() == 0) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty] // CHECK-FIXES: {{^ }}if (str.empty()){{$}} if ((str + str2).size() == 0) ; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if ((str + str2).empty()){{$}} + if ((str + str2).length() == 0) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty] // CHECK-FIXES: {{^ }}if ((str + str2).empty()){{$}} if (str == "") ; @@ -141,7 +154,11 @@ // CHECK-FIXES: {{^ }}if ((str + str2).empty()){{$}} if (wstr.size() == 0) ; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if (wstr.empty()){{$}} + if (wstr.length() == 0) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty] // CHECK-FIXES: {{^ }}if (wstr.empty()){{$}} if (wstr == "") ; @@ -150,7 +167,7 @@ std::vector vect; if (vect.size() == 0) ; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] // CHECK-FIXES: {{^ }}if (vect.empty()){{$}} if (vect == std::vector()) ; @@ -423,17 +440,17 @@ if (templated_container.size()) ; // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used - // CHECK-MESSAGES: :44:8: note: method 'TemplatedContainer'::empty() defined here + // CHECK-MESSAGES: :45:8: note: method 'TemplatedContainer'::empty() defined here // CHECK-FIXES: {{^ }}if (!templated_container.empty()){{$}} if (templated_container != TemplatedContainer()) ; // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used - // CHECK-MESSAGES: :44:8: note: method 'TemplatedContainer'::empty() defined here + // CHECK-MESSAGES: :45:8: note: method 'TemplatedContainer'::empty() defined here // CHECK-FIXES: {{^ }}if (!templated_container.empty()){{$}} // CHECK-FIXES-NEXT: ; CHECKSIZE(templated_container); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used - // CHECK-MESSAGES: :44:8: note: method 'TemplatedContainer'::empty() defined here + // CHECK-MESSAGES: :45:8: note: method 'TemplatedContainer'::empty() defined here // CHECK-FIXES: CHECKSIZE(templated_container); }