diff options
author | Gabor Horvath <xazax.hun@gmail.com> | 2016-04-19 13:29:05 +0000 |
---|---|---|
committer | Gabor Horvath <xazax.hun@gmail.com> | 2016-04-19 13:29:05 +0000 |
commit | 533c01d9b1ef57b4758609de8459a137b233d3e3 (patch) | |
tree | 1988decdc7dc71f13533db7f12817c5ad25c0f38 | |
parent | 0c6d7c0a2c7e227338fe3fd7d397f210dd0b6b20 (diff) | |
download | bcm5719-llvm-533c01d9b1ef57b4758609de8459a137b233d3e3.tar.gz bcm5719-llvm-533c01d9b1ef57b4758609de8459a137b233d3e3.zip |
[clang-tidy] readability-container-size-empty fixes
Summary: This patch fixes PR27410 and adds std::basic_string support.
Reviewers: Eugene.Zelenko, hokein
Subscribers: cfe-commits, o.gyorgy
Differential Revision: http://reviews.llvm.org/D19262
llvm-svn: 266734
-rw-r--r-- | clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp | 20 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp | 23 |
2 files changed, 35 insertions, 8 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp index f194b47ad90..56396156e6a 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -16,6 +16,7 @@ using namespace clang::ast_matchers; static bool isContainerName(llvm::StringRef ClassName) { static const char *const ContainerNames[] = {"array", + "basic_string", "deque", "forward_list", "list", @@ -59,14 +60,13 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) { return; const auto WrongUse = anyOf( - hasParent( - binaryOperator( - anyOf(has(integerLiteral(equals(0))), - allOf(anyOf(hasOperatorName("<"), hasOperatorName(">="), - hasOperatorName(">"), hasOperatorName("<=")), - hasEitherOperand( - ignoringImpCasts(integerLiteral(equals(1))))))) - .bind("SizeBinaryOp")), + hasParent(binaryOperator( + anyOf(hasOperatorName("<"), hasOperatorName(">="), + hasOperatorName(">"), hasOperatorName("<="), + hasOperatorName("=="), hasOperatorName("!=")), + hasEitherOperand(ignoringImpCasts(anyOf( + integerLiteral(equals(1)), integerLiteral(equals(0)))))) + .bind("SizeBinaryOp")), hasParent(implicitCastExpr( hasImplicitDestinationType(booleanType()), anyOf( @@ -122,6 +122,10 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) { if (Value > 1) return; + if (Value == 1 && (OpCode == BinaryOperatorKind::BO_EQ || + OpCode == BinaryOperatorKind::BO_NE)) + return; + // Always true, no warnings for that. if ((OpCode == BinaryOperatorKind::BO_GE && Value == 0 && ContainerIsLHS) || (OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS)) diff --git a/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp b/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp index eb453fcbb09..a3f63857e10 100644 --- a/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp +++ b/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp @@ -7,6 +7,15 @@ template <typename T> struct vector { bool empty() const; }; +template <typename T> struct basic_string { + basic_string(); + unsigned long size() const; + bool empty() const; +}; + +typedef basic_string<char> string; +typedef basic_string<wchar_t> wstring; + inline namespace __v2 { template <typename T> struct set { set(); @@ -20,10 +29,24 @@ template <typename T> struct set { int main() { std::set<int> intSet; + std::string str; + std::wstring wstr; + str.size() + 0; + str.size() - 0; + 0 + str.size(); + 0 - str.size(); 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()){{$}} + if (str.size() == 0) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used + // CHECK-FIXES: {{^ }}if (str.empty()){{$}} + if (wstr.size() == 0) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used + // CHECK-FIXES: {{^ }}if (wstr.empty()){{$}} std::vector<int> vect; if (vect.size() == 0) ; |