diff options
author | Piotr Zegar <me@piotrzegar.pl> | 2024-01-14 15:49:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-14 15:49:16 +0100 |
commit | 7f1d757fb40f06cc1c6b134d770987b340286996 (patch) | |
tree | 167a4b79f56fd93355883eef3c654b78e0caefa2 /clang-tools-extra/test/clang-tidy/checkers/readability | |
parent | 59e79f0de59d9e4576b6bf562de40a914702efd4 (diff) | |
download | llvm-7f1d757fb40f06cc1c6b134d770987b340286996.zip llvm-7f1d757fb40f06cc1c6b134d770987b340286996.tar.gz llvm-7f1d757fb40f06cc1c6b134d770987b340286996.tar.bz2 |
[clang-tidy] Fix false-positives in readability-container-size-empty (#74140)
Added support for size-like method returning signed type, and corrected
false positive caused by always-false check for size bellow zero.
Closes #72619
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/checkers/readability')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp index 3b9e060..84bdbd5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp @@ -33,7 +33,7 @@ class TemplatedContainer { public: bool operator==(const TemplatedContainer<T>& other) const; bool operator!=(const TemplatedContainer<T>& other) const; - int size() const; + unsigned long size() const; bool empty() const; }; @@ -42,7 +42,7 @@ class PrivateEmpty { public: bool operator==(const PrivateEmpty<T>& other) const; bool operator!=(const PrivateEmpty<T>& other) const; - int size() const; + unsigned long size() const; private: bool empty() const; }; @@ -61,7 +61,7 @@ struct EnumSize { class Container { public: bool operator==(const Container& other) const; - int size() const; + unsigned long size() const; bool empty() const; }; @@ -70,13 +70,13 @@ class Derived : public Container { class Container2 { public: - int size() const; + unsigned long size() const; bool empty() const { return size() == 0; } }; class Container3 { public: - int size() const; + unsigned long size() const; bool empty() const; }; @@ -85,7 +85,7 @@ bool Container3::empty() const { return this->size() == 0; } class Container4 { public: bool operator==(const Container4& rhs) const; - int size() const; + unsigned long size() const; bool empty() const { return *this == Container4(); } }; @@ -815,3 +815,49 @@ bool testNotEmptyStringLiterals(const std::string& s) using namespace std::string_literals; return s == "foo"s; } + +namespace PR72619 { + struct SS { + bool empty() const; + int size() const; + }; + + struct SU { + bool empty() const; + unsigned size() const; + }; + + void f(const SU& s) { + if (s.size() < 0) {} + if (0 > s.size()) {} + if (s.size() >= 0) {} + if (0 <= s.size()) {} + if (s.size() < 1) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if (s.empty()){{$}} + if (1 > s.size()) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if (s.empty()){{$}} + if (s.size() <= 0) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if (s.empty()){{$}} + if (0 >= s.size()) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] + // CHECK-FIXES: {{^ }}if (s.empty()){{$}} + } + + void f(const SS& s) { + if (s.size() < 0) {} + if (0 > s.size()) {} + if (s.size() >= 0) {} + if (0 <= s.size()) {} + if (s.size() < 1) {} + if (1 > s.size()) {} + if (s.size() <= 0) {} + if (0 >= s.size()) {} + } +} |