-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEqualsUsesInstanceOf.ql
More file actions
36 lines (33 loc) · 1.26 KB
/
EqualsUsesInstanceOf.ql
File metadata and controls
36 lines (33 loc) · 1.26 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
/**
* @name Possible inconsistency due to instanceof in equals
* @description Implementations of 'equals' that use 'instanceof'
* to test the type of the argument and are further overridden in a subclass
* are likely to violate the 'equals' contract.
* @kind problem
* @problem.severity warning
* @precision low
* @id java/instanceof-in-equals
* @tags reliability
* correctness
*/
import java
predicate instanceofInEquals(EqualsMethod m, InstanceOfExpr e) {
m.fromSource() and
e.getEnclosingCallable() = m and
e.getExpr().(VarAccess).getVariable() = m.getParameter() and
exists(RefType instanceofType |
instanceofType = e.getSyntacticCheckedType() and
not instanceofType.isFinal()
)
}
from EqualsMethod m, InstanceOfExpr e, EqualsMethod m2
where
(instanceofInEquals(m, e) or instanceofInEquals(m2, e)) and
not m.getDeclaringType() instanceof TypeObject and
exists(m.getBody()) and
m2.fromSource() and
exists(Method overridden | overridden.getSourceDeclaration() = m | m2.overrides+(overridden))
select e,
"Possible violation of equals contract due to use of instanceof in $@ and/or overriding $@.", m,
m.getDeclaringType().getName() + "." + m.getName(), m2,
m2.getDeclaringType().getName() + "." + m2.getName()