Skip to content

Commit 097c23e

Browse files
committed
Python: add inline expectations test
Consider removing the original test
1 parent d834cec commit 097c23e

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

python/ql/test/query-tests/Functions/ModificationOfParameterWithDefault/test.expected

Whitespace-only changes.
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# Not OK
22
def simple(l = [0]):
3-
l[0] = 1 # FN
3+
l[0] = 1 #$ MISSING: modification=l
44
return l
55

66
# Not OK
77
def slice(l = [0]):
8-
l[0:1] = 1 # FN
8+
l[0:1] = 1 #$ MISSING: modification=l
99
return l
1010

1111
# Not OK
1212
def list_del(l = [0]):
13-
del l[0] # FN
13+
del l[0] #$ MISSING: modification=l
1414
return l
1515

1616
# Not OK
1717
def append_op(l = []):
18-
l += 1
18+
l += 1 #$ modification=l
1919
return l
2020

2121
# Not OK
2222
def repeat_op(l = [0]):
23-
l *= 3
23+
l *= 3 #$ modification=l
2424
return l
2525

2626
# Not OK
2727
def append(l = []):
28-
l.append(1)
28+
l.append(1) #$ modification=l
2929
return l
3030

3131
# OK
@@ -36,7 +36,7 @@ def includes(l = []):
3636
return x
3737

3838
def extends(l):
39-
l.extend([1])
39+
l.extend([1]) #$ modification=l
4040
return l
4141

4242
# Not OK
@@ -46,17 +46,17 @@ def deferred(l = []):
4646

4747
# Not OK
4848
def nonempty(l = [5]):
49-
l.append(1)
49+
l.append(1) #$ modification=l
5050
return l
5151

5252
# Not OK
5353
def dict(d = {}):
54-
d['a'] = 1 # FN
54+
d['a'] = 1 #$ MISSING: modification=d
5555
return d
5656

5757
# Not OK
5858
def dict_nonempty(d = {'a': 1}):
59-
d['a'] = 2 # FN
59+
d['a'] = 2 #$ MISSING: modification=d
6060
return d
6161

6262
# OK
@@ -65,7 +65,7 @@ def dict_nonempty_nochange(d = {'a': 1}):
6565
return d
6666

6767
def modifies(d):
68-
d['a'] = 1 # FN
68+
d['a'] = 1 #$ MISSING: modification=d
6969
return d
7070

7171
# Not OK
@@ -75,21 +75,21 @@ def dict_deferred(d = {}):
7575

7676
# Not OK
7777
def dict_method(d = {}):
78-
d.update({'a': 1})
78+
d.update({'a': 1}) #$ modification=d
7979
return d
8080

8181
# Not OK
8282
def dict_method_nonempty(d = {'a': 1}):
83-
d.update({'a': 2})
83+
d.update({'a': 2}) #$ modification=d
8484
return d
8585

8686
# OK
8787
def dict_method_nonempty_nochange(d = {'a': 1}):
88-
d.update({'a': 1}) # FP
88+
d.update({'a': 1}) #$ SPURIOUS:modification=d
8989
return d
9090

9191
def modifies_method(d):
92-
d.update({'a': 1})
92+
d.update({'a': 1}) #$ modification=d
9393
return d
9494

9595
# Not OK
@@ -106,23 +106,23 @@ def dict_includes(d = {}):
106106

107107
# Not OK
108108
def dict_del(d = {'a': 1}):
109-
del d['a'] # FN
109+
del d['a'] #$ MISSING: modification=d
110110
return d
111111

112112
# Not OK
113113
def dict_update_op(d = {}):
114114
x = {'a': 1}
115-
d |= x
115+
d |= x #$ modification=d
116116
return d
117117

118118
# OK
119119
def dict_update_op_nochange(d = {}):
120120
x = {}
121-
d |= x # FP
121+
d |= x #$ SPURIOUS: modification=d
122122
return d
123123

124124
# OK
125125
def sanitizer(l = []):
126126
if not l == []:
127-
l.append(1) # FP
127+
l.append(1) #$ SPURIOUS: modification=l
128128
return l
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import python
2+
import semmle.python.dataflow.new.DataFlow
3+
import TestUtilities.InlineExpectationsTest
4+
import experimental.dataflow.TestUtil.PrintNode
5+
import semmle.python.functions.ModificationOfParameterWithDefault
6+
7+
class ModificationOfParameterWithDefaultTest extends InlineExpectationsTest {
8+
ModificationOfParameterWithDefaultTest() { this = "ModificationOfParameterWithDefaultTest" }
9+
10+
override string getARelevantTag() { result = "modification" }
11+
12+
predicate relevant_node(DataFlow::Node sink) {
13+
exists(ModificationOfParameterWithDefault::Configuration cfg | cfg.hasFlowTo(sink))
14+
}
15+
16+
override predicate hasActualResult(Location location, string element, string tag, string value) {
17+
exists(DataFlow::Node n | relevant_node(n) |
18+
n.getLocation() = location and
19+
tag = "modification" and
20+
value = prettyNode(n) and
21+
element = n.toString()
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)