1 // RUN: %check_clang_tidy %s abseil-string-find-startswith %t -- \
2 // RUN:   -config="{CheckOptions: [{key: 'abseil-string-find-startswith.StringLikeClasses', value: '::std::basic_string;::basic_string'}]}"
3 
4 namespace std {
5 template <typename T> class allocator {};
6 template <typename T> class char_traits {};
7 template <typename C, typename T = std::char_traits<C>,
8           typename A = std::allocator<C>>
9 struct basic_string {
10   basic_string();
11   basic_string(const basic_string &);
12   basic_string(const C *, const A &a = A());
13   ~basic_string();
14   int find(basic_string<C> s, int pos = 0);
15   int find(const char *s, int pos = 0);
16 };
17 typedef basic_string<char> string;
18 typedef basic_string<wchar_t> wstring;
19 
20 struct cxx_string {
21   int find(const char *s, int pos = 0);
22 };
23 } // namespace std
24 
25 struct basic_string : public std::cxx_string {
26   basic_string();
27 };
28 typedef basic_string global_string;
29 
30 std::string foo(std::string);
31 std::string bar();
32 
33 #define A_MACRO(x, y) ((x) == (y))
34 
tests(std::string s,global_string s2)35 void tests(std::string s, global_string s2) {
36   s.find("a") == 0;
37   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith instead of find() == 0 [abseil-string-find-startswith]
38   // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, "a");{{$}}
39 
40   s.find(s) == 0;
41   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
42   // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, s);{{$}}
43 
44   s.find("aaa") != 0;
45   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
46   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(s, "aaa");{{$}}
47 
48   s.find(foo(foo(bar()))) != 0;
49   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
50   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(s, foo(foo(bar())));{{$}}
51 
52   if (s.find("....") == 0) { /* do something */ }
53   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use absl::StartsWith
54   // CHECK-FIXES: {{^[[:space:]]*}}if (absl::StartsWith(s, "....")) { /* do something */ }{{$}}
55 
56   0 != s.find("a");
57   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
58   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(s, "a");{{$}}
59 
60   s2.find("a") == 0;
61   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
62   // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s2, "a");{{$}}
63 
64   // expressions that don't trigger the check are here.
65   A_MACRO(s.find("a"), 0);
66   s.find("a", 1) == 0;
67   s.find("a", 1) == 1;
68   s.find("a") == 1;
69 }
70