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

Detect multi-statement lines in else removal #9748

Merged
merged 1 commit into from
Jan 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,9 @@ def bar9():
y = "b"
else:
y = "c"


# Regression test for: https://github.com/astral-sh/ruff/issues/9732
def sb(self):
if self._sb is not None: return self._sb
else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,8 @@ fn remove_else(

// If the statement is on the same line as the `else`, just remove the `else: `.
// Ex) `else: return True` -> `return True`
if let [first] = elif_else.body.as_slice() {
if indexer.in_multi_statement_line(first, locator) {
if let Some(first) = elif_else.body.first() {
if indexer.preceded_by_multi_statement_line(first, locator) {
return Ok(Fix::safe_edit(Edit::deletion(
elif_else.start(),
first.start(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,13 @@ RET505.py:183:5: RET505 Unnecessary `else` after `return` statement
|
= help: Remove unnecessary `else`

RET505.py:200:5: RET505 Unnecessary `else` after `return` statement
|
198 | def sb(self):
199 | if self._sb is not None: return self._sb
200 | else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
| ^^^^ RET505
|
= help: Remove unnecessary `else`


Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,20 @@ RET505.py:183:5: RET505 [*] Unnecessary `else` after `return` statement
186 185 |
187 186 | x = 0

RET505.py:200:5: RET505 [*] Unnecessary `else` after `return` statement
|
198 | def sb(self):
199 | if self._sb is not None: return self._sb
200 | else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
| ^^^^ RET505
|
= help: Remove unnecessary `else`

Safe fix
197 197 | # Regression test for: https://github.com/astral-sh/ruff/issues/9732
198 198 | def sb(self):
199 199 | if self._sb is not None: return self._sb
200 |- else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
200 |+ self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';


Loading