Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rule to detect modification of lists while editing
- Loading branch information
1 parent
28e6687
commit dcf2c5a
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |