-
Notifications
You must be signed in to change notification settings - Fork 2k
[ruff] New rule unnecessary-if (RUF050)
#24114
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
Merged
MichaReiser
merged 2 commits into
astral-sh:main
from
seroperson:i13929-needless-if-rule
Mar 25, 2026
+886
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
crates/ruff_linter/resources/test/fixtures/ruff/RUF050.py
This file contains hidden or 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,124 @@ | ||
| ### Errors (condition removed entirely) | ||
|
|
||
| # Simple if with pass | ||
| if True: | ||
| pass | ||
|
|
||
| # Simple if with ellipsis | ||
| if True: | ||
| ... | ||
|
|
||
| # Side-effect-free condition (comparison) | ||
| import sys | ||
| if sys.version_info >= (3, 11): | ||
| pass | ||
|
|
||
| # Side-effect-free condition (boolean operator) | ||
| if x and y: | ||
| pass | ||
|
|
||
| # Nested in function | ||
| def nested(): | ||
| if a: | ||
| pass | ||
|
|
||
| # Single-line form (pass) | ||
| if True: pass | ||
|
|
||
| # Single-line form (ellipsis) | ||
| if True: ... | ||
|
|
||
| # Multiple pass statements | ||
| if True: | ||
| pass | ||
| pass | ||
|
|
||
| # Mixed pass and ellipsis | ||
| if True: | ||
| pass | ||
| ... | ||
|
|
||
| # Only statement in a with block | ||
| with pytest.raises(ValueError, match=msg): | ||
| if obj1: | ||
| pass | ||
|
|
||
|
|
||
| ### Errors (condition preserved as expression statement) | ||
|
|
||
| # Function call | ||
| if foo(): | ||
| pass | ||
|
|
||
| # Method call | ||
| if bar.baz(): | ||
| pass | ||
|
|
||
| # Nested call in boolean operator | ||
| if x and foo(): | ||
| pass | ||
|
|
||
| # Walrus operator with call | ||
| if (x := foo()): | ||
| pass | ||
|
|
||
| # Walrus operator without call | ||
| if (x := y): | ||
| pass | ||
|
|
||
| # Only statement in a suite | ||
| class Foo: | ||
| if foo(): | ||
| pass | ||
|
|
||
|
|
||
| ### No errors | ||
|
|
||
| # Non-empty body | ||
| if True: | ||
| bar() | ||
|
|
||
| # Body with non-stub statement | ||
| if True: | ||
| pass | ||
| foo() | ||
|
|
||
| # Has elif clause (handled by RUF047) | ||
| if True: | ||
| pass | ||
| elif True: | ||
| pass | ||
|
|
||
| # Has else clause (handled by RUF047) | ||
| if True: | ||
| pass | ||
| else: | ||
| pass | ||
|
|
||
| # TYPE_CHECKING block (handled by TC005) | ||
| from typing import TYPE_CHECKING | ||
| if TYPE_CHECKING: | ||
| pass | ||
|
|
||
| # Comment in body | ||
| if True: | ||
| # comment | ||
| pass | ||
|
|
||
| # Inline comment after pass | ||
| if True: | ||
| pass # comment | ||
|
|
||
| # Inline comment on if line | ||
| if True: # comment | ||
| pass | ||
|
|
||
| # Trailing comment at body indentation | ||
| if True: | ||
| pass | ||
| # trailing comment | ||
|
|
||
| # Trailing comment deeper than body indentation | ||
| if True: | ||
| pass | ||
| # deeper trailing comment | ||
18 changes: 18 additions & 0 deletions
18
crates/ruff_linter/resources/test/fixtures/ruff/RUF050_F401.py
This file contains hidden or 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 @@ | ||
| # Reproduces the scenario from issue #9472: | ||
| # F401 removes unused imports leaving empty `if` blocks, | ||
| # RUF050 removes those blocks, then F401 cleans up the | ||
| # now-unused guard imports on subsequent fix iterations. | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| # F401 removes the unused `ExceptionGroup` import, leaving `pass`. | ||
| # Then RUF050 removes the empty `if`, and F401 removes `sys`. | ||
| if sys.version_info < (3, 11): | ||
| from exceptiongroup import ExceptionGroup | ||
|
|
||
| # Already-empty block handled in a single pass by RUF050 | ||
| if sys.version_info < (3, 11): | ||
| pass | ||
|
|
||
| print(os.getcwd()) |
31 changes: 31 additions & 0 deletions
31
crates/ruff_linter/resources/test/fixtures/ruff/RUF050_RUF047.py
This file contains hidden or 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,31 @@ | ||
| ### Errors (both RUF047 and RUF050 converge) | ||
|
|
||
| # RUF047 removes the else, then RUF050 removes the remaining if. | ||
| if sys.version_info >= (3, 11): | ||
| pass | ||
| else: | ||
| pass | ||
|
|
||
| # Same with elif. | ||
| if sys.version_info >= (3, 11): | ||
| pass | ||
| elif sys.version_info >= (3, 10): | ||
| pass | ||
| else: | ||
| pass | ||
|
|
||
| # Side-effect in condition: RUF047 removes the else, then RUF050 | ||
| # replaces the remaining `if` with the condition expression | ||
| if foo(): | ||
| pass | ||
| else: | ||
| pass | ||
|
|
||
|
|
||
| ### No errors | ||
|
|
||
| # Non-empty if body: neither rule fires. | ||
| if sys.version_info >= (3, 11): | ||
| bar() | ||
| else: | ||
| baz() |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an example where the
ifis the only statement within a suite like what we see in https://github.com/pandas-dev/pandas/blob/389051d090579550f1a1259855f486d7f7e2159e/pandas/tests/generic/test_generic.py#L148Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added, but this intentionally kind of "wrongly" (specifically for this case) results in:
Python's
ifstatement callsobj1.__bool__()to evaluate the condition, and pandas overrides__bool__onDataFrameto raiseValueError. That's what this check asserts (that you can't doif some_data_frame:checks in your code).With
RUF050auto-fix this test will fail and I'm not sure what we can do about it (I've mentioned this in PR's description initially).Shortly, in current implementation the situation is so:
if Var: passentirely.if foo(): passresult infoo().if Var: passis actuallyif Var.__bool__(): pass, which has side-effect and logically must result inVar.__bool__(). But that's weird.So we either should consider this limitation intended and expect users to set
# noqa: RUF050where they expect some side-effect from__bool__()(which is quite very edge case to be honest), or maybe make the whole rule unsafe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's mention this caveat in the documentation. This should be very uncommon and we can always change it if many users are running into this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know when you updated the documentation so that we can merge this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaReiser done