xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-member.cpp (revision bbcb625798514f1cd6ef04818381d38ea26b23e5)
1 // RUN: %check_clang_tidy %s modernize-use-std-format %t -- \
2 // RUN:   -config="{CheckOptions: \
3 // RUN:             { \
4 // RUN:               modernize-use-std-format.StrFormatLikeFunctions: 'MyClass::StrFormat', \
5 // RUN:               modernize-use-std-format.ReplacementFormatFunction: 'format', \
6 // RUN:             } \
7 // RUN:            }" \
8 // RUN:   -- -isystem %clang_tidy_headers
9 
10 #include <cstdio>
11 #include <string.h>
12 #include <string>
13 
14 struct MyClass
15 {
16   template <typename S, typename... Args>
17   std::string StrFormat(const S &format, const Args&... args);
18 };
19 
20 std::string StrFormat_simple(MyClass &myclass, MyClass *pmyclass) {
21   std::string s;
22 
23   s += myclass.StrFormat("MyClass::StrFormat dot %d", 42);
24   // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'format' instead of 'StrFormat' [modernize-use-std-format]
25   // CHECK-FIXES: s += myclass.format("MyClass::StrFormat dot {}", 42);
26 
27   s += pmyclass->StrFormat("MyClass::StrFormat pointer %d", 43);
28   // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'format' instead of 'StrFormat' [modernize-use-std-format]
29   // CHECK-FIXES: s += pmyclass->format("MyClass::StrFormat pointer {}", 43);
30 
31   s += (*pmyclass).StrFormat("MyClass::StrFormat deref pointer %d", 44);
32   // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'format' instead of 'StrFormat' [modernize-use-std-format]
33   // CHECK-FIXES: s += (*pmyclass).format("MyClass::StrFormat deref pointer {}", 44);
34 
35   return s;
36 }
37 
38 struct MyDerivedClass : public MyClass {};
39 
40 std::string StrFormat_derived(MyDerivedClass &derived) {
41   return derived.StrFormat("MyDerivedClass::StrFormat dot %d", 42);
42   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'format' instead of 'StrFormat' [modernize-use-std-format]
43   // CHECK-FIXES: return derived.format("MyDerivedClass::StrFormat dot {}", 42);
44 }
45