1 // RUN: %check_clang_tidy %s llvm-prefer-isa-or-dyn-cast-in-conditionals %t
2 
3 struct X;
4 struct Y;
5 struct Z {
6   int foo();
7   X *bar();
8   X *cast(Y*);
9   bool baz(Y*);
10 };
11 
12 template <class X, class Y>
13 bool isa(Y *);
14 template <class X, class Y>
15 X *cast(Y *);
16 template <class X, class Y>
17 X *dyn_cast(Y *);
18 template <class X, class Y>
19 X *dyn_cast_or_null(Y *);
20 
foo(Y * y,Z * z)21 bool foo(Y *y, Z *z) {
22   if (auto x = cast<X>(y))
23     return true;
24   // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: cast<> in conditional will assert rather than return a null pointer [llvm-prefer-isa-or-dyn-cast-in-conditionals]
25   // CHECK-FIXES: if (auto x = dyn_cast<X>(y))
26 
27   while (auto x = cast<X>(y))
28     break;
29   // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: cast<> in conditional
30   // CHECK-FIXES: while (auto x = dyn_cast<X>(y))
31 
32   if (cast<X>(y))
33     return true;
34   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: cast<> in conditional
35   // CHECK-FIXES: if (isa<X>(y))
36 
37   while (cast<X>(y))
38     break;
39   // CHECK-MESSAGES: :[[@LINE-2]]:10: warning: cast<> in conditional
40   // CHECK-FIXES: while (isa<X>(y))
41 
42   do {
43     break;
44   } while (cast<X>(y));
45   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: cast<> in conditional
46   // CHECK-FIXES: while (isa<X>(y));
47 
48   if (dyn_cast<X>(y))
49     return true;
50   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: return value from dyn_cast<> not used [llvm-prefer-isa-or-dyn-cast-in-conditionals]
51   // CHECK-FIXES: if (isa<X>(y))
52 
53   while (dyn_cast<X>(y))
54     break;
55   // CHECK-MESSAGES: :[[@LINE-2]]:10: warning: return value from dyn_cast<> not used
56   // CHECK-FIXES: while (isa<X>(y))
57 
58   do {
59     break;
60   } while (dyn_cast<X>(y));
61   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: return value from dyn_cast<> not used
62   // CHECK-FIXES: while (isa<X>(y));
63 
64   if (y && isa<X>(y))
65     return true;
66   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred over an explicit test for null followed by calling isa<> [llvm-prefer-isa-or-dyn-cast-in-conditionals]
67   // CHECK-FIXES: if (isa_and_nonnull<X>(y))
68 
69   if (z->bar() && isa<Y>(z->bar()))
70     return true;
71   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning:  isa_and_nonnull<> is preferred
72   // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar()))
73 
74   if (z->bar() && cast<Y>(z->bar()))
75     return true;
76   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
77   // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar()))
78 
79   if (z->bar() && dyn_cast<Y>(z->bar()))
80     return true;
81   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
82   // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar()))
83 
84   if (z->bar() && dyn_cast_or_null<Y>(z->bar()))
85     return true;
86   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
87   // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar()))
88 
89   bool b = z->bar() && cast<Y>(z->bar());
90   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: isa_and_nonnull<> is preferred
91   // CHECK-FIXES: bool b = isa_and_nonnull<Y>(z->bar());
92 
93   // These don't trigger a warning.
94   if (auto x = cast<Z>(y)->foo())
95     return true;
96   if (auto x = z->cast(y))
97     return true;
98   while (auto x = cast<Z>(y)->foo())
99     break;
100   if (cast<Z>(y)->foo())
101     return true;
102   if (z->cast(y))
103     return true;
104   while (cast<Z>(y)->foo())
105     break;
106   if (y && cast<X>(z->bar()))
107     return true;
108   if (z && cast<Z>(y)->foo())
109     return true;
110   bool b2 = y && cast<X>(z);
111   if(z->cast(y))
112     return true;
113   if (z->baz(cast<Y>(z)))
114     return true;
115 
116 #define CAST(T, Obj) cast<T>(Obj)
117 #define AUTO_VAR_CAST(X, Y, Z) auto X = cast<Y>(Z)
118 #define ISA(T, Obj) isa<T>(Obj)
119 #define ISA_OR_NULL(T, Obj) Obj &&isa<T>(Obj)
120 
121   // Macros don't trigger warning.
122   if (auto x = CAST(X, y))
123     return true;
124   if (AUTO_VAR_CAST(x, X, z))
125     return true;
126   if (z->bar() && ISA(Y, z->bar()))
127     return true;
128   if (ISA_OR_NULL(Y, z->bar()))
129     return true;
130 
131   return false;
132 }
133