-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCompareIdenticalValues.ql
More file actions
50 lines (46 loc) · 1.52 KB
/
CompareIdenticalValues.ql
File metadata and controls
50 lines (46 loc) · 1.52 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
45
46
47
48
49
50
/**
* @name Comparison of identical values
* @description When the same variable occurs on both sides of a comparison
* operator, the behavior is always constant, and probably not
* what it was intended to be.
* @kind problem
* @problem.severity warning
* @precision high
* @id cs/comparison-of-identical-expressions
* @tags reliability
*/
import csharp
import semmle.code.csharp.commons.Assertions
import semmle.code.csharp.commons.ComparisonTest
import semmle.code.csharp.commons.Constants
predicate isBoxingTestCase(StaticEqualsCallComparisonTest ct) {
ct.isReferenceEquals()
and
exists(TypeParameter tp |
tp = ct.getFirstArgument().stripCasts().getType() and
not tp.isValueType() and
not tp.isRefType()
)
}
predicate isMutatingOperation(Expr e)
{
e.(MethodCall).getTarget().hasName("Pop")
or
e.(MethodCall).getTarget().hasName("Push")
or
e instanceof MutatorOperation
}
from ComparisonTest ct, Expr e, string msg
where comparesIdenticalValues(ct)
and e = ct.getExpr()
and not isBoxingTestCase(ct)
and (
exists(string m | comparesIdenticalValuesNan(ct, m) | msg = "Comparison is equivalent to using " + m)
or
not comparesIdenticalValuesNan(ct, _) and msg = "Comparison of identical values."
)
and not isMutatingOperation(ct.getAnArgument().getAChild*())
and not isConstantCondition(e, _) // Avoid overlap with cs/constant-condition
and not isConstantComparison(e, _) // Avoid overlap with cs/constant-comparison
and not isExprInAssertion(e)
select ct, msg