Skip to content

Commit 9854b95

Browse files
committed
Fix query performance
1 parent bd50ed9 commit 9854b95

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

csharp/ql/src/Likely Bugs/NestedLoopsSameVariable.ql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class NestedForConditions extends SC::StructuralComparisonConfiguration {
2727
}
2828
}
2929

30+
private predicate hasChild(Stmt outer, Element child) {
31+
outer = child.getParent()
32+
or
33+
hasChild(outer, child.getParent())
34+
}
35+
3036
/** A nested `for` statement that shares the same iteration variable as an outer `for` statement. */
3137
class NestedForLoopSameVariable extends ForStmt {
3238
ForStmt outer;
@@ -35,7 +41,7 @@ class NestedForLoopSameVariable extends ForStmt {
3541
MutatorOperation outerUpdate;
3642

3743
NestedForLoopSameVariable() {
38-
outer = this.getParent+() and
44+
hasChild(outer, this) and
3945
innerUpdate = this.getAnUpdate() and
4046
outerUpdate = outer.getAnUpdate() and
4147
innerUpdate.getOperand() = iteration.getAnAccess() and
@@ -88,7 +94,7 @@ class NestedForLoopSameVariable extends ForStmt {
8894

8995
/** Finds elements inside the outer loop that are no longer guarded by the loop invariant. */
9096
private ControlFlow::Node getAnUnguardedNode() {
91-
result.getElement().getParent+() = getOuterForStmt().getBody() and
97+
hasChild(getOuterForStmt().getBody(), result.getElement()) and
9298
(
9399
result =
94100
this.getCondition().(ControlFlowElement).getAControlFlowExitNode().getAFalseSuccessor()

csharp/ql/src/Likely Bugs/UncheckedCastInEquals.ql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
import csharp
1313
import semmle.code.csharp.frameworks.System
1414

15+
private predicate equalsMethodChild(EqualsMethod equals, Element child) {
16+
child = equals
17+
or
18+
equalsMethodChild(equals, child.getParent())
19+
}
20+
1521
predicate nodeBeforeParameterAccess(ControlFlow::Node node) {
1622
exists(EqualsMethod equals | equals.getBody() = node.getElement())
1723
or
1824
exists(EqualsMethod equals, Parameter param, ControlFlow::Node mid |
1925
equals.getParameter(0) = param and
20-
equals.getAChild*() = mid.getElement() and
26+
equalsMethodChild(equals, mid.getElement()) and
2127
nodeBeforeParameterAccess(mid) and
2228
not param.getAnAccess() = mid.getElement() and
2329
mid.getASuccessor() = node

0 commit comments

Comments
 (0)