Skip to content

Commit

Permalink
add rule to detect modification of lists while editing
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewDennison committed Feb 20, 2020
1 parent 28e6687 commit dcf2c5a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/smells/list-modify-iterating.py
@@ -0,0 +1,19 @@
l = list(range(100))
for i in l:
print(i),
# ruleid:list-pop-while-iterate
print(l.pop(0))
# ruleid:list-pop-while-iterate
x = l.pop(0)
print(x)

a = [1, 2, 3, 4]
for i in a:
print(i)
# ruleid:list-pop-while-iterate
a.pop(0)

for i in a:
print(i)
# ruleid:list-pop-while-iterate
a.append(0)
55 changes: 55 additions & 0 deletions python/smells/list-modify-iterating.yaml
@@ -0,0 +1,55 @@
rules:
- id: list-pop-while-iterate
patterns:
- pattern-either:
- pattern: |
for $ELEMENT in $LIST:
...
$LIST.pop(...)
- pattern: |
for $ELEMENT in $LIST:
...
$LIST.push(...)
- pattern: |
for $ELEMENT in $LIST:
...
$LIST.append(...)
- pattern: |
for $ELEMENT in $LIST:
...
$LIST.extend(...)
- pattern: |
for $ELEMENT in $LIST:
...
$X = $LIST.pop(...)
- pattern: |
for $ELEMENT in $LIST:
...
$X = $LIST.push(...)
- pattern: |
for $ELEMENT in $LIST:
...
$X = $LIST.append(...)
- pattern: |
for $ELEMENT in $LIST:
...
$X = $LIST.extend(...)
- pattern: |
for $ELEMENT in $LIST:
...
$X($LIST.pop(...))
- pattern: |
for $ELEMENT in $LIST:
...
$X($LIST.push(...))
- pattern: |
for $ELEMENT in $LIST:
...
$X($LIST.append(...))
- pattern: |
for $ELEMENT in $LIST:
...
$X($LIST.extend(...))
message: "It appears that `$LIST` is a list that is being modified while in a for loop. This is usually a bad idea"
languages: [python]
severity: WARNING

0 comments on commit dcf2c5a

Please sign in to comment.