Skip to content

Commit ff6e0ce

Browse files
committed
Python: UnguardedNextInGenerator.ql: Excludes next with default value
1 parent 2f3967c commit ff6e0ce

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

python/ql/src/Exceptions/UnguardedNextInGenerator.ql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ predicate call_to_next(CallNode call, ControlFlowNode iter) {
2828
iter = next().getArgumentForCall(call, 0)
2929
}
3030

31+
predicate call_to_next_has_default(CallNode call) {
32+
exists(call.getArg(1))
33+
}
34+
3135
predicate guarded_not_empty_sequence(EssaVariable sequence) {
3236
sequence.getDefinition() instanceof EssaEdgeRefinement
3337
}
@@ -49,6 +53,7 @@ predicate stop_iteration_handled(CallNode call) {
4953

5054
from CallNode call
5155
where call_to_next(call, _) and
56+
not call_to_next_has_default(call) and
5257
not exists(EssaVariable iterator |
5358
call_to_next(call, iterator.getAUse()) and
5459
iter_not_exhausted(iterator)

python/ql/test/query-tests/Exceptions/generators/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ def next_in_comp(seq, fields):
4747
seq_iter = iter(seq)
4848
values = [ next(seq_iter) if f.attname in NAMES else DEFAULT for f in fields ]
4949
return values
50+
51+
def ok5(seq):
52+
yield next(iter([]), 'foo')

0 commit comments

Comments
 (0)