-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUselessIsBeforeAs.ql
More file actions
54 lines (49 loc) · 1.41 KB
/
UselessIsBeforeAs.ql
File metadata and controls
54 lines (49 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @name Useless 'is' before 'as'
* @description The C# 'as' operator performs a type test - there is no need to precede it with an 'is'.
* @kind problem
* @problem.severity recommendation
* @precision medium
* @id cs/useless-is-before-as
* @tags maintainability
* language-features
* external/cwe/cwe-561
*/
import csharp
import semmle.code.csharp.commons.StructuralComparison
class StructuralComparisonConfig extends StructuralComparisonConfiguration {
StructuralComparisonConfig() {
this = "UselessIsBeforeAs"
}
override predicate candidate(Element x, Element y) {
exists(IfStmt is, AsExpr ae, IsTypeExpr ie |
ie = is.getCondition().getAChild*()
and
ae.getTargetType() = ie.getCheckedType()
and
x = ie.getExpr()
and
y = ae.getExpr()
|
ae = is.getThen().getAChild*()
or
ae = is.getElse().getAChild*()
)
}
predicate uselessIsBeforeAs(AsExpr ae, IsExpr ie) {
exists(Expr x, Expr y |
same(x, y)
and
ie.getExpr() = x
and
ae.getExpr() = y
)
}
}
from AsExpr ae, IsExpr ie
where
exists(StructuralComparisonConfig c | c.uselessIsBeforeAs(ae, ie))
and
not exists(MethodCall mc | ae = mc.getAnArgument().getAChildExpr*())
select ae, "This 'as' expression performs a type test - it should be directly compared against null, rendering the 'is' $@ potentially redundant.",
ie, "here"