| 1 | // RUN: %check_clang_tidy %s readability-string-compare %t -- -- -isystem %clang_tidy_headers |
| 2 | #include <string> |
| 3 | |
| 4 | void func(bool b); |
| 5 | |
| 6 | std::string comp() { |
| 7 | std::string str("a" , 1); |
| 8 | return str; |
| 9 | } |
| 10 | |
| 11 | void Test() { |
| 12 | std::string str1("a" , 1); |
| 13 | std::string str2("b" , 1); |
| 14 | |
| 15 | if (str1.compare(str: str2)) { |
| 16 | } |
| 17 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare] |
| 18 | if (!str1.compare(str: str2)) { |
| 19 | } |
| 20 | // CHECK-MESSAGES: [[@LINE-2]]:8: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare] |
| 21 | if (str1.compare(str: str2) == 0) { |
| 22 | } |
| 23 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 24 | // CHECK-FIXES: if (str1 == str2) { |
| 25 | if (str1.compare(str: str2) != 0) { |
| 26 | } |
| 27 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 28 | // CHECK-FIXES: if (str1 != str2) { |
| 29 | if (str1.compare(s: "foo" ) == 0) { |
| 30 | } |
| 31 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 32 | // CHECK-FIXES: if (str1 == "foo") { |
| 33 | if (0 == str1.compare(str: str2)) { |
| 34 | } |
| 35 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 36 | // CHECK-FIXES: if (str2 == str1) { |
| 37 | if (0 != str1.compare(str: str2)) { |
| 38 | } |
| 39 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 40 | // CHECK-FIXES: if (str2 != str1) { |
| 41 | func(b: str1.compare(str: str2)); |
| 42 | // CHECK-MESSAGES: [[@LINE-1]]:8: warning: do not use 'compare' to test equality of strings; |
| 43 | if (str2.empty() || str1.compare(str: str2) != 0) { |
| 44 | } |
| 45 | // CHECK-MESSAGES: [[@LINE-2]]:23: warning: do not use 'compare' to test equality of strings; |
| 46 | // CHECK-FIXES: if (str2.empty() || str1 != str2) { |
| 47 | std::string *str3 = &str1; |
| 48 | if (str3->compare(str: str2)) { |
| 49 | } |
| 50 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 51 | if (str3->compare(str: str2) == 0) { |
| 52 | } |
| 53 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 54 | // CHECK-FIXES: if (*str3 == str2) { |
| 55 | if (str2.compare(str: *str3) == 0) { |
| 56 | } |
| 57 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 58 | // CHECK-FIXES: if (str2 == *str3) { |
| 59 | if (comp().compare(str: str1) == 0) { |
| 60 | } |
| 61 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 62 | // CHECK-FIXES: if (comp() == str1) { |
| 63 | if (str1.compare(str: comp()) == 0) { |
| 64 | } |
| 65 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 66 | // CHECK-FIXES: if (str1 == comp()) { |
| 67 | if (str1.compare(str: comp())) { |
| 68 | } |
| 69 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; |
| 70 | |
| 71 | std::string_view sv1("a" ); |
| 72 | std::string_view sv2("b" ); |
| 73 | if (sv1.compare(str: sv2)) { |
| 74 | } |
| 75 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare] |
| 76 | } |
| 77 | |
| 78 | struct DerivedFromStdString : std::string {}; |
| 79 | |
| 80 | void TestDerivedClass() { |
| 81 | DerivedFromStdString derived; |
| 82 | if (derived.compare(str: derived)) { |
| 83 | } |
| 84 | // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare] |
| 85 | } |
| 86 | |
| 87 | void Valid() { |
| 88 | std::string str1("a" , 1); |
| 89 | std::string str2("b" , 1); |
| 90 | |
| 91 | if (str1 == str2) { |
| 92 | } |
| 93 | if (str1 != str2) { |
| 94 | } |
| 95 | if (str1.compare(str: str2) == str1.compare(str: str2)) { |
| 96 | } |
| 97 | if (0 == 0) { |
| 98 | } |
| 99 | if (str1.compare(str: str2) > 0) { |
| 100 | } |
| 101 | if (str1.compare(pos: 1, n: 3, str: str2)) { |
| 102 | } |
| 103 | if (str1.compare(str: str2) > 0) { |
| 104 | } |
| 105 | if (str1.compare(str: str2) < 0) { |
| 106 | } |
| 107 | if (str1.compare(str: str2) == 2) { |
| 108 | } |
| 109 | if (str1.compare(str: str2) == -3) { |
| 110 | } |
| 111 | if (str1.compare(str: str2) == 1) { |
| 112 | } |
| 113 | if (str1.compare(str: str2) == -1) { |
| 114 | } |
| 115 | |
| 116 | std::string_view sv1("a" ); |
| 117 | std::string_view sv2("b" ); |
| 118 | if (sv1 == sv2) { |
| 119 | } |
| 120 | if (sv1.compare(str: sv2) > 0) { |
| 121 | } |
| 122 | } |
| 123 | |