Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Published Rules - semgrep.check-is-none-explicitly #3480

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions python/correctness/check-is-none-explicitly.py
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")
19 changes: 19 additions & 0 deletions python/correctness/check-is-none-explicitly.yaml
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/
Loading