-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[ruff
] Needless else
clause (RUF047
)
#15051
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF047 | 6 | 6 | 0 | 0 | 0 |
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.
Thanks.
I refactored your rule to remove some duplication and simplified it to only detect single pass
or ...
statements. PIE790 detects bodies containing both a ...
and a pass
statement.
I further extended the test cases and there's one case where we fail to detect a useless else because of the comment and one case where we marked the else as useless when we should not. We have to take the indent of the comments into consideration. See
fn handle_own_line_comment_between_branches<'a>( |
fn handle_own_line_comment_after_branch<'a>( |
Maybe we can come up with a simpler heuristic for this specific case?
.../src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF047_RUF047_if.py.snap
Outdated
Show resolved
Hide resolved
@MichaReiser If indentation is to be taken into consideration, should this be reported or not? if test:
_4_spaces()
# 2 spaces
else:
... What about this? if test:
_1_tab() # These two lines would align if tabs are displayed as 1 character wide.
# 1 space
else:
...
# Same question, but for 2, 3, 4 spaces? |
We should implement it so that it's consistent with the formatter. Your examples get formatted to if test:
_1_tab() # These two lines would align if tabs are displayed as 1 character wide.
# 1 space
else:
...
# Same question, but for 2, 3, 4 spaces?
if test:
_4_spaces()
# 2 spaces
else:
... |
@InSyncWithFoo do you plan to follow up on this PR? |
@MichaReiser Yes. |
Co-authored-by: Micha Reiser <[email protected]>
Took me long enough. I think I got most of it, save for this one: if a:
b()
else:
...
# comment I have no idea how to detect that comment: it's not included within |
I would use the tokens and start at the end of the |
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.
Nice
Summary
Partially addresses #13929.
Test Plan
cargo nextest run
andcargo insta test
.