Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e487fd3

Browse files
committed
Python: Improve alert message for py/iter-returns-non-iterator
Fixes #1427
1 parent 6056b45 commit e487fd3

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

change-notes/1.23/analysis-python.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
| **Query** | **Expected impact** | **Change** |
2020
|----------------------------|------------------------|------------|
2121
| Unreachable code | Fewer false positives | Analysis now accounts for uses of `contextlib.suppress` to suppress exceptions. |
22-
22+
| `__iter__` method returns a non-iterator | Better alert message | Alert now highlights which class is expected to be an iterator. |

python/ql/src/Functions/IterReturnsNonIterator.ql

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
import python
1414

15-
FunctionObject iter_method(ClassObject t) { result = t.lookupAttribute("__iter__") }
16-
1715
cached
1816
ClassObject return_type(FunctionObject f) {
1917
exists(ControlFlowNode n, Return ret |
@@ -23,12 +21,12 @@ ClassObject return_type(FunctionObject f) {
2321
)
2422
}
2523

26-
from ClassObject t, FunctionObject iter
24+
from ClassObject container, FunctionObject iter, ClassObject iterator
2725
where
28-
exists(ClassObject ret_t |
29-
iter = iter_method(t) and
30-
ret_t = return_type(iter) and
31-
not ret_t.isIterator()
32-
)
33-
select iter, "The '__iter__' method of iterable class $@ does not return an iterator.", t,
34-
t.getName()
26+
iter = container.lookupAttribute("__iter__") and
27+
iterator = return_type(iter) and
28+
not iterator.isIterator()
29+
select iterator,
30+
"Class " + iterator.getName() +
31+
" is returned as an iterator (by $@) but does not fully implement the iterator interface.",
32+
iter, iter.getName()

0 commit comments

Comments
 (0)