Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from returntocorp/feature/more-rules-and-tests
Feature/more rules and tests
- Loading branch information
Showing
36 changed files
with
734 additions
and
6 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
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,4 @@ | ||
test: | ||
sgrep-lint --validate --config=./python . | ||
sgrep-lint --validate --config=./c . | ||
./test.py --ignore-todo . |
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,17 @@ | ||
import os | ||
|
||
# ruleid:python37-compatability-os-module | ||
os.pwrite('a') | ||
|
||
if hasattr(os, 'pwrite'): | ||
# OK | ||
os.pwrite('a') | ||
|
||
|
||
if hasattr(os, 'pwritev'): | ||
# OK | ||
os.pwritev('a') | ||
|
||
|
||
# ruleid:python37-compatibility-os2-ok2 | ||
os.pwritev('b') |
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 @@ | ||
|
||
class A: | ||
def method1(self, args): | ||
pass | ||
|
||
|
||
class A2: | ||
def method2(self, args): | ||
pass | ||
|
||
|
||
class B: | ||
def method1(self, args): | ||
print('hello there') | ||
|
||
# todoruleid: baseclass-attribute-override | ||
class C(A, B): | ||
def __init__(): | ||
print('initialized') |
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,17 @@ | ||
rules: | ||
- id: baseclass-attribute-override | ||
patterns: | ||
- pattern: | | ||
class $A(...): | ||
def $F(...): | ||
... | ||
... | ||
class $B(...): | ||
def $F(...): | ||
... | ||
... | ||
class $C(..., $A, $B, ...): | ||
... | ||
message: "Class $C inherits from both `$A` and `$B` which both have a method named `$F`; one of these methods will be overwritten" | ||
languages: [python] | ||
severity: WARNING |
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,3 @@ | ||
x = y | ||
print('hi') | ||
print('now {x} is {y}') |
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,10 @@ | ||
rules: | ||
- id: should-be-fstring | ||
patterns: | ||
- pattern: | | ||
$X = $Y | ||
... | ||
print('...{$X}...') | ||
message: "possibly missing an f-string specifier for string containing variable $X" | ||
languages: [python] | ||
severity: WARNING |
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 @@ | ||
|
||
# ruleid: return-not-in-function | ||
return 5 | ||
|
||
|
||
def alwaysblue(): | ||
if isblue(): | ||
return 'blue' | ||
# todoruleid: code-after-unconditional-return | ||
return 'red' | ||
return 'green' | ||
|
||
|
||
def alwaysblue(): | ||
if isblue(): | ||
return 'blue' | ||
# todoruleid: code-after-unconditional-return | ||
return 'red' | ||
x = 5 |
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,18 @@ | ||
rules: | ||
- id: code-after-unconditional-return | ||
patterns: | ||
- pattern: | | ||
return $X | ||
$S | ||
message: "code after return statement will not be executed" | ||
languages: [python] | ||
severity: WARNING | ||
- id: return-not-in-function | ||
patterns: | ||
- pattern-not-inside: | | ||
def $F(...): | ||
... | ||
- pattern: return $X | ||
message: "`return` only makes sense inside a function" | ||
languages: [python] | ||
severity: WARNING |
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,23 @@ | ||
d = {} | ||
z = {} | ||
a = {} | ||
for i in xrange(100): | ||
# ruleid: useless-assignment-keyed | ||
d[i] = z[i] | ||
d[i] = z[i] | ||
d[i+1] = z[i] | ||
|
||
for i in xrange(100): | ||
# todoruleid: useless-assignment-keyed | ||
da[i*1][j] = z[i] | ||
da[i*1][j] = z[i] | ||
da[i*4] = z[i] | ||
|
||
# todoruleid: useless-assignment | ||
x = 5 | ||
x = 5 | ||
|
||
x = y | ||
x = y() | ||
|
||
y() = y() |
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,21 @@ | ||
rules: | ||
- id: useless-assignment-keyed | ||
patterns: | ||
- pattern-either: | ||
- pattern: | | ||
$X[$Y] = ... | ||
$X[$Y] = ... | ||
- pattern: | | ||
$X[$Y][$Z] = ... | ||
$X[$Y][$Z] = ... | ||
message: "key `$Y` in `$X` is uselessly assigned the same value twice" | ||
languages: [python] | ||
severity: WARNING | ||
- id: useless-assignment | ||
patterns: | ||
- pattern: | | ||
$X = $Y | ||
$X = $Y | ||
message: "`$X` is uselessly assigned to the same value (`$Y`) twice" | ||
languages: [python] | ||
severity: WARNING |
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
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,39 @@ | ||
a, b, c = 1 | ||
|
||
# ruleid: useless-if-conditional | ||
if a: | ||
print('1') | ||
elif a: | ||
print('2') | ||
|
||
# ruleid: useless-if-body | ||
if a: | ||
print('1') | ||
else: | ||
print('1') | ||
|
||
# ruleid: useless-if-body | ||
if a: | ||
print('1') | ||
elif b: | ||
print('1') | ||
|
||
|
||
# todoruleid: useless-if-body | ||
if a: | ||
print('this is a') | ||
elif b: | ||
print('this is b') | ||
elif c: | ||
print('this is c') | ||
elif d: | ||
print('this is d') | ||
|
||
|
||
if a: | ||
print('this is a') | ||
# ruleid: useless-if-body | ||
elif b: | ||
print('this is b') | ||
elif c: | ||
print('this is b') |
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,35 @@ | ||
rules: | ||
- id: useless-if-conditional | ||
patterns: | ||
- pattern-either: | ||
- pattern: | | ||
if $X: | ||
... | ||
elif $X: | ||
... | ||
message: "if block checks for the same condition on both branches (`$X`)" | ||
languages: [python] | ||
severity: WARNING | ||
- id: useless-if-body | ||
patterns: | ||
- pattern-either: | ||
- pattern: | | ||
if $X: | ||
$S | ||
elif $Y: | ||
$S | ||
- pattern: | | ||
if $X: | ||
$S | ||
else: | ||
$S | ||
- pattern: | | ||
if $X: | ||
$S | ||
elif $Z: | ||
... | ||
elif $Y: | ||
$S | ||
message: "useless if statment; both blocks have the same body" | ||
languages: [python] | ||
severity: WARNING |
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,21 @@ | ||
# todoruleid:useless-inner-function | ||
def A(): | ||
print_error('test') | ||
|
||
def B(): | ||
print_error('again') | ||
|
||
def C(): | ||
print_error('another') | ||
return None | ||
|
||
# todoruleid:useless-inner-function | ||
def A(): | ||
print_error('test') | ||
|
||
def B(): | ||
print_error('again') | ||
|
||
def C(): | ||
print_error('another') | ||
return B(), C() |
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 @@ | ||
rules: | ||
- id: useless-inner-function | ||
patterns: | ||
- pattern-not-inside: | | ||
def $F(...): | ||
... | ||
def $F2(...): | ||
... | ||
... | ||
$F2 | ||
- pattern: | | ||
def $F(...): | ||
... | ||
def $F2(...): | ||
... | ||
... | ||
message: "function `$F2` is defined inside a function but never used" | ||
languages: [python] | ||
severity: ERROR |
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,12 @@ | ||
# todoruleid: useless-literal-dict | ||
d = dict(1: 'a', 2: 'b', 1: 'a') | ||
# todoruleid: useless-literal-set | ||
d = set(1: 'a', 2: 'b', 1: 'a') | ||
|
||
# todoruleid: useless-literal-dict | ||
d = {1: 'a', 2: 'b', 1: 'a'} | ||
# todoruleid: useless-literal-dict | ||
d = {'a': 1, 'a': 1} | ||
|
||
# OK | ||
d = {1: 'a', 2: 'b', 3: 'a'} |
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,18 @@ | ||
rules: | ||
- id: useless-literal-dict | ||
patterns: | ||
- pattern-either: | ||
# - pattern: | | ||
# dict(..., $X: $A, ..., $X: $B, ...) | ||
- pattern: | | ||
{..., $X: $A, ..., $X: $B, ...} | ||
message: "key `$X` is uselessly assigned twice" | ||
languages: [python] | ||
severity: WARNING | ||
- id: useless-literal-set | ||
patterns: | ||
# - pattern: | | ||
# set(..., $X: $A, ..., $X: $B, ...) | ||
message: "`$X` is uselessly assigned twice inside the creation of the set" | ||
languages: [python] | ||
severity: ERROR |
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,24 @@ | ||
# ruleid:raise-not-base-exception | ||
raise "error here" | ||
|
||
# todoruleid:raise-not-base-exception | ||
raise 5 | ||
|
||
|
||
class Foobar: | ||
x = 5 | ||
|
||
|
||
# todoruleid:raise-not-base-exception | ||
raise Foobar() | ||
|
||
|
||
class Foobar2(BaseException): | ||
x = 5 | ||
|
||
|
||
# OK | ||
raise Foobar2() | ||
|
||
# OK | ||
raise Exception() |
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,16 @@ | ||
rules: | ||
- id: raise-not-base-exception | ||
patterns: | ||
- pattern-either: | ||
- pattern: raise "..." | ||
#- pattern: | | ||
# raise $X: int | ||
#- pattern: | | ||
# raise $X: float | ||
# TODO, the second pattern requires sgrep typing support | ||
- pattern: | | ||
$X: BaseException | ||
raise $X(...) | ||
message: "In Python3, a runtime `TypeError` will be thrown if you attempt to raise an object or class which does not inherit from `BaseException`" | ||
languages: [python] | ||
severity: ERROR |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,13 @@ | ||
|
||
# todoruleid:missing-hash-with-eq | ||
class A: | ||
def __eq__(self, someother): | ||
pass | ||
|
||
|
||
class A2: | ||
def __eq__(self, someother): | ||
pass | ||
|
||
def __hash__(self): | ||
pass |
Oops, something went wrong.