Skip to content

Commit 55ed8e2

Browse files
committed
Python: Port NonIteratorInForLoop.ql
Same comment as for the preceding commit. Test changes are due to the fact that we're now not selecting `ControlFlowNode`s directly.
1 parent a406fd3 commit 55ed8e2

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

python/ql/src/Statements/NonIteratorInForLoop.ql

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
*/
1313

1414
import python
15-
private import LegacyPointsTo
15+
private import semmle.python.dataflow.new.internal.DataFlowDispatch
1616

17-
from For loop, ControlFlowNodeWithPointsTo iter, Value v, ClassValue t, ControlFlowNode origin
17+
from For loop, Expr iter, Class cls
1818
where
19-
loop.getIter().getAFlowNode() = iter and
20-
iter.pointsTo(_, v, origin) and
21-
v.getClass() = t and
22-
not t.isIterable() and
23-
not t.failedInference(_) and
24-
not v = Value::named("None") and
25-
not t.isDescriptorType()
26-
select loop, "This for-loop may attempt to iterate over a $@ of class $@.", origin,
27-
"non-iterable instance", t, t.getName()
19+
iter = loop.getIter() and
20+
classInstanceTracker(cls).asExpr() = iter and
21+
not DuckTyping::isIterable(cls) and
22+
not DuckTyping::isDescriptor(cls) and
23+
not DuckTyping::hasUnresolvedBase(getADirectSuperclass*(cls))
24+
select loop, "This for-loop may attempt to iterate over a $@ of class $@.", iter,
25+
"non-iterable instance", cls, cls.getName()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.py:50:1:50:23 | For | This for-loop may attempt to iterate over a $@ of class $@. | test.py:50:10:50:22 | ControlFlowNode for NonIterator() | non-iterable instance | test.py:45:1:45:26 | class NonIterator | NonIterator |
1+
| test.py:50:1:50:23 | For | This for-loop may attempt to iterate over a $@ of class $@. | test.py:50:10:50:22 | NonIterator() | non-iterable instance | test.py:45:1:45:26 | Class NonIterator | NonIterator |

0 commit comments

Comments
 (0)