Skip to content

Commit b4f2fc6

Browse files
committed
C#: Use Gvn comparison instead of StructuralComparisonConfiguration in SelfAssignment.
1 parent f241eef commit b4f2fc6

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

csharp/ql/src/Likely Bugs/SelfAssignment.ql

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,32 @@
1313
import csharp
1414
import semmle.code.csharp.commons.StructuralComparison
1515

16-
class StructuralComparisonConfig extends StructuralComparisonConfiguration {
17-
StructuralComparisonConfig() { this = "SelfAssignment" }
18-
19-
override predicate candidate(ControlFlowElement x, ControlFlowElement y) {
20-
exists(AssignExpr ae |
21-
// Member initializers are never self-assignments, in particular
22-
// not initializers such as `new C { F = F };`
23-
not ae instanceof MemberInitializer and
24-
// Enum field initializers are never self assignments. `enum E { A = 42 }`
25-
not ae.getParent().(Field).getDeclaringType() instanceof Enum
26-
|
27-
ae.getLValue() = x and
28-
ae.getRValue() = y
29-
) and
30-
forall(Expr e | e = x.(Expr).getAChildExpr*() |
31-
// Non-trivial property accesses may have side-effects,
32-
// so these are not considered
33-
e instanceof PropertyAccess implies e instanceof TrivialPropertyAccess
34-
)
35-
}
16+
private predicate candidate(AssignExpr ae) {
17+
// Member initializers are never self-assignments, in particular
18+
// not initializers such as `new C { F = F };`
19+
not ae instanceof MemberInitializer and
20+
// Enum field initializers are never self assignments. `enum E { A = 42 }`
21+
not ae.getParent().(Field).getDeclaringType() instanceof Enum and
22+
forall(Expr e | e = ae.getLValue().getAChildExpr*() |
23+
// Non-trivial property accesses may have side-effects,
24+
// so these are not considered
25+
e instanceof PropertyAccess implies e instanceof TrivialPropertyAccess
26+
)
27+
}
3628

37-
AssignExpr getSelfAssignExpr() {
38-
exists(Expr x, Expr y |
39-
same(x, y) and
40-
result.getLValue() = x and
41-
result.getRValue() = y
42-
)
43-
}
29+
private predicate selfAssignExpr(AssignExpr ae) {
30+
candidate(ae) and
31+
sameGvn(ae.getLValue(), ae.getRValue())
4432
}
4533

46-
Declaration getDeclaration(Expr e) {
34+
private Declaration getDeclaration(Expr e) {
4735
result = e.(VariableAccess).getTarget()
4836
or
4937
result = e.(MemberAccess).getTarget()
5038
or
5139
result = getDeclaration(e.(ArrayAccess).getQualifier())
5240
}
5341

54-
from StructuralComparisonConfig c, AssignExpr ae, Declaration target
55-
where ae = c.getSelfAssignExpr() and target = getDeclaration(ae.getLValue())
42+
from AssignExpr ae, Declaration target
43+
where selfAssignExpr(ae) and target = getDeclaration(ae.getLValue())
5644
select ae, "This assignment assigns $@ to itself.", target, target.getName()

0 commit comments

Comments
 (0)