Skip to content

Commit 6f5b2e8

Browse files
committed
C#: Use Gvn comparison instead of StructuralComparisonConfiguration in UseTryGetValue.
1 parent aada8d3 commit 6f5b2e8

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

csharp/ql/lib/semmle/code/csharp/commons/StructuralComparison.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ private import Cached
190190

191191
predicate toGvn = toGvnCached/1;
192192

193+
/**
194+
* Holds if the control flow elements `x` and `y` are structurally equal.
195+
*/
193196
pragma[inline]
194-
private predicate sameGvn(ControlFlowElement x, ControlFlowElement y) {
197+
predicate sameGvn(ControlFlowElement x, ControlFlowElement y) {
195198
pragma[only_bind_into](toGvn(pragma[only_bind_out](x))) =
196199
pragma[only_bind_into](toGvn(pragma[only_bind_out](y)))
197200
}

csharp/ql/src/Performance/UseTryGetValue.ql

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@ import csharp
1313
import semmle.code.csharp.commons.StructuralComparison
1414
import semmle.code.csharp.controlflow.Guards as G
1515

16-
class SameElement extends StructuralComparisonConfiguration {
17-
SameElement() { this = "Same element" }
18-
19-
override predicate candidate(ControlFlowElement e1, ControlFlowElement e2) {
20-
exists(MethodCall mc, IndexerRead access |
21-
mc.getTarget().hasName("ContainsKey") and
22-
access.getQualifier().(G::GuardedExpr).isGuardedBy(mc, mc.getQualifier(), _) and
23-
e1 = mc.getArgument(0) and
24-
e2 = access.getIndex(0)
25-
)
26-
}
16+
pragma[noinline]
17+
private predicate candidate(MethodCall mc, IndexerRead access) {
18+
mc.getTarget().hasName("ContainsKey") and
19+
access.getQualifier().(G::GuardedExpr).isGuardedBy(mc, mc.getQualifier(), _)
2720
}
2821

29-
from SameElement element, MethodCall call, IndexerAccess index
30-
where element.same(call.getArgument(0), index.getIndex(0))
22+
from MethodCall call, IndexerRead index
23+
where
24+
candidate(call, index) and
25+
sameGvn(call.getArgument(0), index.getIndex(0))
3126
select call, "Inefficient use of 'ContainsKey' and $@.", index, "indexer"

0 commit comments

Comments
 (0)