-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUselessNullCoalescingExpression.ql
More file actions
44 lines (39 loc) · 1.27 KB
/
UselessNullCoalescingExpression.ql
File metadata and controls
44 lines (39 loc) · 1.27 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
/**
* @name Useless ?? expression
* @description The null-coalescing operator is intended to help provide special handling for the case when a variable is null - its two operands should
* always do different things.
* @kind problem
* @problem.severity error
* @precision medium
* @id cs/coalesce-of-identical-expressions
* @tags quality
* maintainability
* useless-code
* external/cwe/cwe-561
*/
import csharp
import semmle.code.csharp.commons.StructuralComparison
pragma[nomagic]
private predicate relevant(Expr left, Expr right) {
exists(NullCoalescingOperation nce |
left = nce.getLeftOperand() and
right = nce.getRightOperand()
)
}
pragma[noinline]
private predicate same(AssignableAccess x, AssignableAccess y) {
exists(Expr e |
relevant(x, e) and
y = e.getAChildExpr*() and
sameGvn(x, y)
)
}
private predicate uselessNullCoalescingOperation(NullCoalescingOperation nce) {
exists(AssignableAccess x |
nce.getLeftOperand() = x and
forex(AssignableAccess y | same(x, y) | y instanceof AssignableRead and not y.isRefArgument())
)
}
from NullCoalescingOperation nce
where uselessNullCoalescingOperation(nce)
select nce, "Both operands of this null-coalescing expression access the same variable or property."