You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Clang-Tidy] Handle nested-name-specifier in "llvm-prefer-isa-or-dyn-cast-in-conditionals" (#155982)
Use `declRefExpr` matcher to match callee so that we can get the
`SourceRange` of the identifier of the callee for replacement.
Drive-by changes:
- Use `hasConditionVariableStatement` matcher to handle `if` statements
with init-statement.
- Support `for` loops.
Fixes#154790
Copy file name to clipboardExpand all lines: clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-isa-or-dyn-cast-in-conditionals.cpp
+61Lines changed: 61 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -9,21 +9,47 @@ struct Z {
9
9
boolbaz(Y*);
10
10
};
11
11
12
+
namespacellvm {
12
13
template <classX, classY>
13
14
boolisa(Y *);
14
15
template <classX, classY>
15
16
X *cast(Y *);
16
17
template <classX, classY>
18
+
X *cast_or_null(Y *);
19
+
template <classX, classY>
17
20
X *dyn_cast(Y *);
18
21
template <classX, classY>
19
22
X *dyn_cast_or_null(Y *);
23
+
} // namespace llvm
24
+
25
+
usingnamespacellvm;
20
26
21
27
boolfoo(Y *y, Z *z) {
22
28
if (auto x = cast<X>(y))
23
29
returntrue;
24
30
// 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
31
// CHECK-FIXES: if (auto x = dyn_cast<X>(y))
26
32
33
+
if (auto x = ::cast<X>(y))
34
+
returntrue;
35
+
// CHECK-MESSAGES: :[[@LINE-2]]:18: warning: cast<> in conditional will assert rather than return a null pointer [llvm-prefer-isa-or-dyn-cast-in-conditionals]
36
+
// CHECK-FIXES: if (auto x = ::dyn_cast<X>(y))
37
+
38
+
if (auto x = llvm::cast<X>(y))
39
+
returntrue;
40
+
// CHECK-MESSAGES: :[[@LINE-2]]:22: warning: cast<> in conditional will assert rather than return a null pointer [llvm-prefer-isa-or-dyn-cast-in-conditionals]
41
+
// CHECK-FIXES: if (auto x = llvm::dyn_cast<X>(y))
42
+
43
+
if (auto x = ::llvm::cast<X>(y))
44
+
returntrue;
45
+
// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: cast<> in conditional will assert rather than return a null pointer [llvm-prefer-isa-or-dyn-cast-in-conditionals]
46
+
// CHECK-FIXES: if (auto x = ::llvm::dyn_cast<X>(y))
47
+
48
+
for (; auto x = cast<X>(y); )
49
+
break;
50
+
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: cast<> in conditional
51
+
// CHECK-FIXES: for (; auto x = dyn_cast<X>(y); )
52
+
27
53
while (auto x = cast<X>(y))
28
54
break;
29
55
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: cast<> in conditional
@@ -34,6 +60,16 @@ bool foo(Y *y, Z *z) {
34
60
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: cast<> in conditional
35
61
// CHECK-FIXES: if (isa<X>(y))
36
62
63
+
if (auto x = cast<X>(y); cast<X>(y))
64
+
returntrue;
65
+
// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: cast<> in conditional will assert rather than return a null pointer [llvm-prefer-isa-or-dyn-cast-in-conditionals]
66
+
// CHECK-FIXES: if (auto x = cast<X>(y); isa<X>(y))
67
+
68
+
for (; cast<X>(y); )
69
+
break;
70
+
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: cast<> in conditional
71
+
// CHECK-FIXES: for (; isa<X>(y); )
72
+
37
73
while (cast<X>(y))
38
74
break;
39
75
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: cast<> in conditional
@@ -50,6 +86,11 @@ bool foo(Y *y, Z *z) {
50
86
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: return value from dyn_cast<> not used [llvm-prefer-isa-or-dyn-cast-in-conditionals]
51
87
// CHECK-FIXES: if (isa<X>(y))
52
88
89
+
for (; dyn_cast<X>(y); )
90
+
break;
91
+
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: return value from dyn_cast<> not used
92
+
// CHECK-FIXES: for (; isa<X>(y); )
93
+
53
94
while (dyn_cast<X>(y))
54
95
break;
55
96
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: return value from dyn_cast<> not used
@@ -66,6 +107,21 @@ bool foo(Y *y, Z *z) {
66
107
// 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
108
// CHECK-FIXES: if (isa_and_nonnull<X>(y))
68
109
110
+
if (y && ::isa<X>(y))
111
+
returntrue;
112
+
// 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]
113
+
// CHECK-FIXES: if (::isa_and_nonnull<X>(y))
114
+
115
+
if (y && llvm::isa<X>(y))
116
+
returntrue;
117
+
// 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]
118
+
// CHECK-FIXES: if (llvm::isa_and_nonnull<X>(y))
119
+
120
+
if (y && ::llvm::isa<X>(y))
121
+
returntrue;
122
+
// 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]
123
+
// CHECK-FIXES: if (::llvm::isa_and_nonnull<X>(y))
124
+
69
125
if (z->bar() && isa<Y>(z->bar()))
70
126
returntrue;
71
127
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
@@ -76,6 +132,11 @@ bool foo(Y *y, Z *z) {
76
132
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
77
133
// CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar()))
78
134
135
+
if (z->bar() && cast_or_null<Y>(z->bar()))
136
+
returntrue;
137
+
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
138
+
// CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar()))
139
+
79
140
if (z->bar() && dyn_cast<Y>(z->bar()))
80
141
returntrue;
81
142
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred
0 commit comments