xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/llvm/twine-local.cpp (revision 89a1d03e2b379e325daa5249411e414bbd995b5e)
1*89a1d03eSRichard // RUN: %check_clang_tidy %s llvm-twine-local %t
2*89a1d03eSRichard 
3*89a1d03eSRichard namespace llvm {
4*89a1d03eSRichard class Twine {
5*89a1d03eSRichard public:
6*89a1d03eSRichard   Twine(const char *);
7*89a1d03eSRichard   Twine(int);
8*89a1d03eSRichard   Twine();
9*89a1d03eSRichard   Twine &operator+(const Twine &);
10*89a1d03eSRichard };
11*89a1d03eSRichard }
12*89a1d03eSRichard 
13*89a1d03eSRichard using namespace llvm;
14*89a1d03eSRichard 
15*89a1d03eSRichard void foo(const Twine &x);
16*89a1d03eSRichard void bar(Twine x);
17*89a1d03eSRichard 
18*89a1d03eSRichard static Twine Moo = Twine("bark") + "bah";
19*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: twine variables are prone to use-after-free bugs
20*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
21*89a1d03eSRichard // CHECK-FIXES: static std::string Moo = (Twine("bark") + "bah").str();
22*89a1d03eSRichard 
main()23*89a1d03eSRichard int main() {
24*89a1d03eSRichard   const Twine t = Twine("a") + "b" + Twine(42);
25*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
26*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
27*89a1d03eSRichard // CHECK-FIXES: std::string t = (Twine("a") + "b" + Twine(42)).str();
28*89a1d03eSRichard   foo(Twine("a") + "b");
29*89a1d03eSRichard 
30*89a1d03eSRichard   Twine Prefix = false ? "__INT_FAST" : "__UINT_FAST";
31*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: twine variables are prone to use-after-free bugs
32*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
33*89a1d03eSRichard // CHECK-FIXES: const char * Prefix = false ? "__INT_FAST" : "__UINT_FAST";
34*89a1d03eSRichard 
35*89a1d03eSRichard   const Twine t2 = Twine();
36*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
37*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
38*89a1d03eSRichard // CHECK-FIXES: std::string t2 = (Twine()).str();
39*89a1d03eSRichard   foo(Twine() + "b");
40*89a1d03eSRichard 
41*89a1d03eSRichard   const Twine t3 = Twine(42);
42*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
43*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
44*89a1d03eSRichard // CHECK-FIXES: std::string t3 = (Twine(42)).str();
45*89a1d03eSRichard 
46*89a1d03eSRichard   const Twine t4 = Twine(42) + "b";
47*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
48*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
49*89a1d03eSRichard // CHECK-FIXES: std::string t4 = (Twine(42) + "b").str();
50*89a1d03eSRichard 
51*89a1d03eSRichard   const Twine t5 = Twine() + "b";
52*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
53*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
54*89a1d03eSRichard // CHECK-FIXES: std::string t5 = (Twine() + "b").str();
55*89a1d03eSRichard 
56*89a1d03eSRichard   const Twine t6 = true ? Twine() : Twine(42);
57*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
58*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
59*89a1d03eSRichard // CHECK-FIXES: std::string t6 = (true ? Twine() : Twine(42)).str();
60*89a1d03eSRichard 
61*89a1d03eSRichard   const Twine t7 = false ? Twine() : Twine("b");
62*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
63*89a1d03eSRichard // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
64*89a1d03eSRichard // CHECK-FIXES: std::string t7 = (false ? Twine() : Twine("b")).str();
65*89a1d03eSRichard }
66