From c6ff9c349d8f2992ca27dfe82750ec251b44f52f Mon Sep 17 00:00:00 2001 From: Gabor Horvath Date: Mon, 21 Dec 2015 09:43:52 +0000 Subject: [PATCH] Fix a false positive case in ContainerSizeEmpty check (PR25893). llvm-svn: 256142 --- clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp | 5 +++++ .../test/clang-tidy/readability-container-size-empty.cpp | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp index 87ba314..c8f7bae 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -126,6 +126,11 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) { (OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS)) return; + // Do not warn for size > 1, 1 < size. + if ((OpCode == BinaryOperatorKind::BO_GT && Value == 1 && ContainerIsLHS) || + (OpCode == BinaryOperatorKind::BO_LT && Value == 1 && !ContainerIsLHS)) + return; + if (OpCode == BinaryOperatorKind::BO_NE && Value == 0) Negation = true; if ((OpCode == BinaryOperatorKind::BO_GT || 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 d7f2612..e43feb9 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 @@ -50,6 +50,10 @@ int main() { ; // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used // CHECK-FIXES: {{^ }}if (!vect.empty()){{$}} + if (vect.size() > 1) // no warning + ; + if (1 < vect.size()) // no warning + ; if (!vect.size()) ; // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: the 'empty' method should be used -- 2.7.4