| 1 | // RUN: %check_clang_tidy %s readability-string-compare %t -- -config='{CheckOptions: {readability-string-compare.StringLikeClasses: "CustomStringTemplateBase;CustomStringNonTemplateBase"}}' -- -isystem %clang_tidy_headers |
| 2 | #include <string> |
| 3 | |
| 4 | struct CustomStringNonTemplateBase { |
| 5 | int compare(const CustomStringNonTemplateBase& Other) const { |
| 6 | return 123; // value is not important for check |
| 7 | } |
| 8 | }; |
| 9 | |
| 10 | template <typename T> |
| 11 | struct CustomStringTemplateBase { |
| 12 | int compare(const CustomStringTemplateBase& Other) const { |
| 13 | return 123; |
| 14 | } |
| 15 | }; |
| 16 | |
| 17 | struct CustomString1 : CustomStringNonTemplateBase {}; |
| 18 | struct CustomString2 : CustomStringTemplateBase<char> {}; |
| 19 | |
| 20 | void CustomStringClasses() { |
| 21 | std::string_view sv1("a" ); |
| 22 | std::string_view sv2("b" ); |
| 23 | if (sv1.compare(str: sv2)) { // No warning - if a std class is not listed in StringLikeClasses, it won't be checked. |
| 24 | } |
| 25 | |
| 26 | CustomString1 custom1; |
| 27 | if (custom1.compare(Other: custom1)) { |
| 28 | } |
| 29 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare] |
| 30 | |
| 31 | CustomString2 custom2; |
| 32 | if (custom2.compare(Other: custom2)) { |
| 33 | } |
| 34 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare] |
| 35 | } |
| 36 | |