1// RUN: %check_clang_tidy %s readability-redundant-string-cstr %t
2
3namespace std {
4template <typename T>
5class allocator {};
6template <typename T>
7class char_traits {};
8template <typename C, typename T, typename A>
9struct basic_string {
10 basic_string();
11 // MSVC headers define two constructors instead of using optional arguments.
12 basic_string(const C *p);
13 basic_string(const C *p, const A &a);
14 const C *c_str() const;
15 const C *data() const;
16};
17typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string;
18}
19namespace llvm {
20struct StringRef {
21 StringRef(const char *p);
22 StringRef(const std::string &);
23};
24}
25
26void f1(const std::string &s) {
27 f1(s: s.c_str());
28 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
29 // CHECK-FIXES: {{^ }}f1(s);{{$}}
30 f1(s: s.data());
31 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
32 // CHECK-FIXES: {{^ }}f1(s);{{$}}
33}
34void f2(const llvm::StringRef r) {
35 std::string s;
36 f2(r: s.c_str());
37 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call {{.*}}
38 // CHECK-FIXES: {{^ }}std::string s;{{$}}
39 // CHECK-FIXES-NEXT: {{^ }}f2(s);{{$}}
40}
41void f3(const llvm::StringRef &r) {
42 std::string s;
43 f3(r: s.c_str());
44 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call {{.*}}
45 // CHECK-FIXES: {{^ }}std::string s;{{$}}
46 // CHECK-FIXES-NEXT: {{^ }}f3(s);{{$}}
47}
48

source code of clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr-msvc.cpp