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