-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Open
InSyncWithFoo
wants to merge
3
commits into
astral-sh:main
Choose a base branch
from
InSyncWithFoo:RUF047
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions
44
crates/ruff_linter/resources/test/fixtures/ruff/RUF047_for.py
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,44 @@ | ||
for _ in range(0): | ||
loop_body_is_not_checked() | ||
break | ||
else: | ||
pass | ||
|
||
|
||
for this in comment: | ||
belongs_to() # `for` | ||
else: | ||
... | ||
|
||
|
||
for this in second_comment: | ||
belongs() # to | ||
# `else` | ||
else: | ||
pass | ||
|
||
|
||
for _and in so: | ||
does() | ||
# this | ||
else: | ||
pass | ||
|
||
|
||
for of in course(): | ||
this() | ||
else: | ||
... # too | ||
|
||
|
||
for of in course(): | ||
this() | ||
else: | ||
... | ||
# too | ||
|
||
for of in course(): | ||
this() | ||
else: | ||
... | ||
# this comment does not belong to the else |
60 changes: 60 additions & 0 deletions
60
crates/ruff_linter/resources/test/fixtures/ruff/RUF047_if.py
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,60 @@ | ||
if False: | ||
condition_is_not_evaluated() | ||
else: | ||
pass | ||
|
||
|
||
if this_comment(): | ||
belongs_to() # `if` | ||
else: | ||
... | ||
|
||
|
||
if elif_is(): | ||
treated() | ||
elif the_same(): | ||
as_if() | ||
else: | ||
pass | ||
|
||
|
||
if this_second_comment(): | ||
belongs() # to | ||
# `else` | ||
else: | ||
pass | ||
|
||
|
||
if this_second_comment(): | ||
belongs() # to | ||
# `else` | ||
else: | ||
pass | ||
|
||
if of_course(): | ||
this() | ||
else: | ||
... # too | ||
|
||
if of_course(): | ||
this() | ||
else: | ||
... | ||
# comment | ||
|
||
if of_course(): | ||
this() | ||
else: | ||
... | ||
# this comment doesn't belong to the if | ||
|
||
if of_course: this() | ||
else: ... | ||
|
||
if of_course: | ||
this() # comment | ||
else: ... | ||
|
||
if of_course: | ||
this() # comment | ||
else: ... # trailing |
71 changes: 71 additions & 0 deletions
71
crates/ruff_linter/resources/test/fixtures/ruff/RUF047_try.py
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,71 @@ | ||
try: | ||
raise try_body_is_not_checked() | ||
except: | ||
pass | ||
else: | ||
pass | ||
|
||
|
||
try: | ||
this() | ||
except comment: | ||
belongs() | ||
except: | ||
to() # `except` | ||
else: | ||
... | ||
|
||
|
||
try: | ||
this() | ||
except (second, comment): | ||
belongs() # to | ||
# `else` | ||
else: | ||
pass | ||
|
||
|
||
try: | ||
and_so() | ||
except: | ||
does() | ||
# this | ||
else: | ||
... | ||
|
||
|
||
try: | ||
of_course() | ||
except: | ||
this() | ||
else: | ||
... # too | ||
|
||
try: | ||
of_course() | ||
except: | ||
this() | ||
else: | ||
... | ||
# This comment belongs to else | ||
finally: | ||
pass | ||
|
||
try: | ||
of_course() | ||
except: | ||
this() | ||
else: | ||
... | ||
# This comment belongs to finally | ||
finally: | ||
pass | ||
|
||
|
||
try: | ||
of_course() | ||
except: | ||
this() | ||
else: | ||
... | ||
# This comment belongs to the statement coming after the else |
44 changes: 44 additions & 0 deletions
44
crates/ruff_linter/resources/test/fixtures/ruff/RUF047_while.py
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,44 @@ | ||
while True: | ||
loop_body_is_not_checked() | ||
break | ||
else: | ||
pass | ||
|
||
|
||
while this_comment: | ||
belongs_to() # `for` | ||
else: | ||
... | ||
|
||
|
||
while this_second_comment: | ||
belongs() # to | ||
# `else` | ||
else: | ||
pass | ||
|
||
|
||
while and_so: | ||
does() | ||
# this | ||
else: | ||
... | ||
|
||
|
||
while of_course(): | ||
this() | ||
else: | ||
... # too | ||
|
||
while of_course(): | ||
this() | ||
else: | ||
... | ||
# this comment belongs to the else | ||
|
||
while of_course(): | ||
this() | ||
else: | ||
... | ||
# this comment belongs to the statement coming after the else | ||
|
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
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 |
---|---|---|
|
@@ -416,6 +416,10 @@ mod tests { | |
#[test_case(Rule::UnnecessaryRegularExpression, Path::new("RUF055_1.py"))] | ||
#[test_case(Rule::UnnecessaryCastToInt, Path::new("RUF046.py"))] | ||
#[test_case(Rule::PytestRaisesAmbiguousPattern, Path::new("RUF043.py"))] | ||
#[test_case(Rule::NeedlessElse, Path::new("RUF047_if.py"))] | ||
#[test_case(Rule::NeedlessElse, Path::new("RUF047_for.py"))] | ||
#[test_case(Rule::NeedlessElse, Path::new("RUF047_while.py"))] | ||
#[test_case(Rule::NeedlessElse, Path::new("RUF047_try.py"))] | ||
Comment on lines
+419
to
+422
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The needless-else rule has no |
||
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> { | ||
let snapshot = format!( | ||
"preview__{}_{}", | ||
|
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
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.
We should detect that this
else
branch is useless because the comment belongs to the if block