-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathSelfAssignment.ql
44 lines (39 loc) · 1.4 KB
/
SelfAssignment.ql
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
/**
* @name Self-assignment
* @description Assigning a variable to itself is useless and very likely indicates an error in the code.
* @kind problem
* @problem.severity error
* @precision high
* @id cs/self-assignment
* @tags reliability
* correctness
* logic
*/
import csharp
import semmle.code.csharp.commons.StructuralComparison
private predicate candidate(AssignExpr ae) {
// Member initializers are never self-assignments, in particular
// not initializers such as `new C { F = F };`
not ae instanceof MemberInitializer and
// Enum field initializers are never self assignments. `enum E { A = 42 }`
not ae.getParent().(Field).getDeclaringType() instanceof Enum and
forall(Expr e | e = ae.getLValue().getAChildExpr*() |
// Non-trivial property accesses may have side-effects,
// so these are not considered
e instanceof PropertyAccess implies e instanceof TrivialPropertyAccess
)
}
private predicate selfAssignExpr(AssignExpr ae) {
candidate(ae) and
sameGvn(ae.getLValue(), ae.getRValue())
}
private Declaration getDeclaration(Expr e) {
result = e.(VariableAccess).getTarget()
or
result = e.(MemberAccess).getTarget()
or
result = getDeclaration(e.(ArrayAccess).getQualifier())
}
from AssignExpr ae, Declaration target
where selfAssignExpr(ae) and target = getDeclaration(ae.getLValue())
select ae, "This assignment assigns $@ to itself.", target, target.getName()