Skip to content

Commit 33f4503

Browse files
authored
Merge pull request github#3213 from RasmusWL/python-iter-str-seq-with-tests
Python: supress non-useful results (w/ tests) for iter str/seq query
2 parents 40def2a + 64c013e commit 33f4503

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

python/ql/src/Statements/IterableStringOrSequence.ql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import python
15+
import semmle.python.filters.Tests
1516

1617
predicate has_string_type(Value v) {
1718
v.getClass() = ClassValue::str()
@@ -28,7 +29,10 @@ where
2829
iter.pointsTo(seq, seq_origin) and
2930
has_string_type(str) and
3031
seq.getClass().isIterable() and
31-
not has_string_type(seq)
32+
not has_string_type(seq) and
33+
// suppress occurrences from tests
34+
not seq_origin.getScope().getScope*() instanceof TestScope and
35+
not str_origin.getScope().getScope*() instanceof TestScope
3236
select loop,
3337
"Iteration over $@, of class " + seq.getClass().getName() + ", may also iterate over $@.",
3438
seq_origin, "sequence", str_origin, "string"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This example illustrates that not all valid results are useful.
2+
# The alert in this file should be suppressed.
3+
# see https://github.com/Semmle/ql/issues/3207
4+
5+
def foo(l):
6+
for (k, v) in l:
7+
print(k, v)
8+
9+
foo([('a', 42), ('b', 43)])
10+
11+
import unittest
12+
13+
class FooTest(unittest.TestCase):
14+
def test_valid(self):
15+
foo([('a', 42), ('b', 43)])
16+
17+
def test_not_valid(self):
18+
with six.assertRaises(self, ValueError):
19+
foo("not valid")

0 commit comments

Comments
 (0)