-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEqualsUsesAs.ql
More file actions
33 lines (31 loc) · 1.17 KB
/
EqualsUsesAs.ql
File metadata and controls
33 lines (31 loc) · 1.17 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
/**
* @name Equals should not apply "as"
* @description Implementations of 'Equals' should not use "as" to test the type of the argument, but rather call GetType(). This guards against the possibility that the argument type will be subclassed. Otherwise, it is likely that the Equals method will not be symmetric, violating its contract.
* @kind problem
* @problem.severity warning
* @precision medium
* @id cs/equals-uses-as
* @tags reliability
* maintainability
*/
import csharp
import semmle.code.csharp.frameworks.System
from EqualsMethod m, AsExpr e, Class asType
where
m.fromSource() and
e.getEnclosingCallable() = m and
e.getExpr().(VariableAccess).getTarget() = m.getParameter(0) and
asType = e.getTargetType() and
not asType.isSealed() and
not exists(MethodCall c, Variable v |
c.getEnclosingCallable() = m and
c.getTarget().getName() = "GetType" and
v = c.getQualifier().(VariableAccess).getTarget()
|
v = m.getParameter(0) or
v.getAnAssignedValue() = e
)
select e,
m.getDeclaringType().getName() +
".Equals(object) should not use \"as\" on its parameter, as it will not work properly for subclasses of "
+ asType.getName() + "."