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 | |
72 | void Valid() { |
73 | std::string str1("a" , 1); |
74 | std::string str2("b" , 1); |
75 | if (str1 == str2) { |
76 | } |
77 | if (str1 != str2) { |
78 | } |
79 | if (str1.compare(str: str2) == str1.compare(str: str2)) { |
80 | } |
81 | if (0 == 0) { |
82 | } |
83 | if (str1.compare(str: str2) > 0) { |
84 | } |
85 | if (str1.compare(pos: 1, n: 3, str: str2)) { |
86 | } |
87 | if (str1.compare(str: str2) > 0) { |
88 | } |
89 | if (str1.compare(str: str2) < 0) { |
90 | } |
91 | if (str1.compare(str: str2) == 2) { |
92 | } |
93 | if (str1.compare(str: str2) == -3) { |
94 | } |
95 | if (str1.compare(str: str2) == 1) { |
96 | } |
97 | if (str1.compare(str: str2) == -1) { |
98 | } |
99 | } |
100 | |