Skip to content

Commit 49ae549

Browse files
committed
Python: Implement modifying syntax
1 parent 097c23e commit 49ae549

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

python/ql/src/semmle/python/functions/ModificationOfParameterWithDefaultCustomizations.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,16 @@ module ModificationOfParameterWithDefault {
9797
*/
9898
class Mutation extends Sink {
9999
Mutation() {
100+
// assignment to a subscript (includes slices)
101+
exists(DefinitionNode d | d.(SubscriptNode).getObject() = this.asCfgNode())
102+
or
103+
// deletion of a subscript
104+
exists(DeletionNode d | d.getTarget().(SubscriptNode).getObject() = this.asCfgNode())
105+
or
106+
// augmented assignment to the value
100107
exists(AugAssign a | a.getTarget().getAFlowNode() = this.asCfgNode())
101108
or
109+
// modifying function call
102110
exists(DataFlow::CallCfgNode c, DataFlow::AttrRead a | c.getFunction() = a |
103111
a.getObject() = this and
104112
a.getAttributeName() in [list_modifying_method(), dict_modifying_method()]

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

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

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

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

1616
# Not OK
@@ -51,21 +51,21 @@ def nonempty(l = [5]):
5151

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

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

6262
# OK
6363
def dict_nonempty_nochange(d = {'a': 1}):
64-
d['a'] = 1
64+
d['a'] = 1 #$ SPURIOUS: modification=d
6565
return d
6666

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

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

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

112112
# Not OK

0 commit comments

Comments
 (0)