aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/test/clang-tidy/checkers/readability
diff options
context:
space:
mode:
authorVadim D <36827317+vvd170501@users.noreply.github.com>2024-05-09 11:25:26 +0300
committerGitHub <noreply@github.com>2024-05-09 10:25:26 +0200
commitfebd89cafea11e6603f593e41be1a21ca9d009ac (patch)
tree6413111007539b901b96994abcd130470005569c /clang-tools-extra/test/clang-tidy/checkers/readability
parent443377a9d1a8d4a69a317a1a892184c59dd0aec6 (diff)
downloadllvm-febd89cafea11e6603f593e41be1a21ca9d009ac.zip
llvm-febd89cafea11e6603f593e41be1a21ca9d009ac.tar.gz
llvm-febd89cafea11e6603f593e41be1a21ca9d009ac.tar.bz2
[clang-tidy] check `std::string_view` and custom string-like classes in `readability-string-compare` (#88636)
This PR aims to expand the list of classes that are considered to be "strings" by `readability-string-compare` check. 1. Currently only `std::string;:compare` is checked, but `std::string_view` has a similar `compare` method. This PR enables checking of `std::string_view::compare` by default. 2. Some codebases use custom string-like classes that have public interfaces similar to `std::string` or `std::string_view`. Example: [TStringBase](https://github.com/yandex/yatool/blob/main/util/generic/strbase.h#L38), A new option, `readability-string-compare.StringClassNames`, is added to allow specifying a custom list of string-like classes. Related to, but does not solve #28396 (only adds support for custom string-like classes, not custom functions)
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/checkers/readability')
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/readability/string-compare-custom-string-classes.cpp35
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/readability/string-compare.cpp23
2 files changed, 58 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare-custom-string-classes.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare-custom-string-classes.cpp
new file mode 100644
index 0000000..faf1358
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare-custom-string-classes.cpp
@@ -0,0 +1,35 @@
+// RUN: %check_clang_tidy %s readability-string-compare %t -- -config='{CheckOptions: {readability-string-compare.StringLikeClasses: "CustomStringTemplateBase;CustomStringNonTemplateBase"}}' -- -isystem %clang_tidy_headers
+#include <string>
+
+struct CustomStringNonTemplateBase {
+ int compare(const CustomStringNonTemplateBase& Other) const {
+ return 123; // value is not important for check
+ }
+};
+
+template <typename T>
+struct CustomStringTemplateBase {
+ int compare(const CustomStringTemplateBase& Other) const {
+ return 123;
+ }
+};
+
+struct CustomString1 : CustomStringNonTemplateBase {};
+struct CustomString2 : CustomStringTemplateBase<char> {};
+
+void CustomStringClasses() {
+ std::string_view sv1("a");
+ std::string_view sv2("b");
+ if (sv1.compare(sv2)) { // No warning - if a std class is not listed in StringLikeClasses, it won't be checked.
+ }
+
+ CustomString1 custom1;
+ if (custom1.compare(custom1)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]
+
+ CustomString2 custom2;
+ if (custom2.compare(custom2)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare.cpp
index 2c08b86..c4fea43 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/string-compare.cpp
@@ -67,11 +67,27 @@ void Test() {
if (str1.compare(comp())) {
}
// CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings;
+
+ std::string_view sv1("a");
+ std::string_view sv2("b");
+ if (sv1.compare(sv2)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]
+}
+
+struct DerivedFromStdString : std::string {};
+
+void TestDerivedClass() {
+ DerivedFromStdString derived;
+ if (derived.compare(derived)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]
}
void Valid() {
std::string str1("a", 1);
std::string str2("b", 1);
+
if (str1 == str2) {
}
if (str1 != str2) {
@@ -96,4 +112,11 @@ void Valid() {
}
if (str1.compare(str2) == -1) {
}
+
+ std::string_view sv1("a");
+ std::string_view sv2("b");
+ if (sv1 == sv2) {
+ }
+ if (sv1.compare(sv2) > 0) {
+ }
}