-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Published Rules - semgrep.check-is-none-explicitly (#3480)
* add semgrep/check-is-none-explicitly.yaml * add semgrep/check-is-none-explicitly.py * move new rule to correctness directory --------- Co-authored-by: Clara McCreery <clara@semgrep.com> Co-authored-by: Vasilii <inkz@xakep.ru>
- Loading branch information
1 parent
ca45011
commit 5aa4f20
Showing
2 changed files
with
38 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 @@ | ||
# ruleid: check-is-none-explicitly | ||
if record and record == 0: | ||
print("hello, this will never happen") | ||
|
||
# ok: check-is-none-explicitly | ||
if record is not None and record == 0: | ||
print("this is fine") | ||
|
||
# ruleid: check-is-none-explicitly | ||
if record.a and record.a == 0: | ||
print("Not reachable") | ||
|
||
# ruleid: check-is-none-explicitly | ||
if record.a.get("H") and record.a["H"] == 0: | ||
print("Not reachable") | ||
|
||
# ok: check-is-none-explicitly | ||
if record.a.get("I") and record.a["J"] == 0: | ||
print("This is also fine") |
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: check-is-none-explicitly | ||
pattern-either: | ||
- pattern: $X and $X == 0 | ||
- pattern: $X.get($FIELD) and $X[$FIELD] == 0 | ||
fix: ($X != None) and $X == 0 | ||
message: This expression will always return False because 0 is a false-y value. | ||
So if $X is 0, then the first part of this expression will return False but if | ||
it is not, the second part will return False. Perhaps you meant to check if $X | ||
was None explicitly. | ||
languages: | ||
- python | ||
severity: WARNING | ||
metadata: | ||
category: correctness | ||
technology: | ||
- none | ||
references: | ||
- https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/ |