Skip to content

Commit 758a5d7

Browse files
committed
few join order fixes
1 parent 6eca4ba commit 758a5d7

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

python/ql/lib/semmle/python/AstExtended.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ abstract class AstNode extends AstNode_ {
5454
}
5555

5656
/** Whether this contains `inner` syntactically */
57+
pragma[nomagic]
5758
predicate contains(AstNode inner) { this.getAChildNode+() = inner }
5859

5960
pragma[noinline]

python/ql/lib/semmle/python/Function.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,19 @@ class Function extends Function_, Scope, AstNode {
167167

168168
/** A def statement. Note that FunctionDef extends Assign as a function definition binds the newly created function */
169169
class FunctionDef extends Assign {
170+
FunctionExpr f;
171+
170172
/* syntax: def name(...): ... */
171173
FunctionDef() {
172174
/* This is an artificial assignment the rhs of which is a (possibly decorated) FunctionExpr */
173-
exists(FunctionExpr f | this.getValue() = f or this.getValue() = f.getADecoratorCall())
175+
this.getValue() = f or this.getValue() = f.getADecoratorCall()
174176
}
175177

176178
override string toString() { result = "FunctionDef" }
177179

178180
/** Gets the function for this statement */
179181
Function getDefinedFunction() {
180-
exists(FunctionExpr func | this.containsInScope(func) and result = func.getInnerScope())
182+
result = f.getInnerScope() // XXX: This behaves very differently. But from inspecting the results of the previous version, that had every function in the same scope as the result.
181183
}
182184

183185
override Stmt getLastStatement() { result = this.getDefinedFunction().getLastStatement() }

python/ql/src/Statements/UnnecessaryDelete.ql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
1414

1515
import python
1616

17+
predicate isInsideLoop(AstNode node) {
18+
node.getParentNode() instanceof While
19+
or
20+
node.getParentNode() instanceof For
21+
or
22+
exists(AstNode prev | isInsideLoop(prev) | node = prev.getAChildNode())
23+
}
24+
1725
from Delete del, Expr e, Function f
1826
where
1927
f.getLastStatement() = del and
2028
e = del.getATarget() and
2129
f.containsInScope(e) and
2230
not e instanceof Subscript and
2331
not e instanceof Attribute and
24-
not exists(Stmt s | s.(While).contains(del) or s.(For).contains(del)) and
32+
not isInsideLoop(del) and
2533
// False positive: calling `sys.exc_info` within a function results in a
2634
// reference cycle, and an explicit call to `del` helps break this cycle.
2735
not exists(FunctionValue ex |

0 commit comments

Comments
 (0)