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

Skip empty lines when determining base indentation #9795

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_return/RET505.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,13 @@ def bar9():
def sb(self):
if self._sb is not None: return self._sb
else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';


def indent(x, y, w, z):
if x: # [no-else-return]
a = 1
return y
else:

c = 3
return z
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,15 @@ RET505.py:200:5: RET505 Unnecessary `else` after `return` statement
|
= help: Remove unnecessary `else`

RET505.py:207:5: RET505 Unnecessary `else` after `return` statement
|
205 | a = 1
206 | return y
207 | else:
| ^^^^ RET505
208 |
209 | c = 3
|
= help: Remove unnecessary `else`


Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,30 @@ RET505.py:200:5: RET505 [*] Unnecessary `else` after `return` statement
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';
201 201 |
202 202 |
203 203 | def indent(x, y, w, z):

RET505.py:207:5: RET505 [*] Unnecessary `else` after `return` statement
|
205 | a = 1
206 | return y
207 | else:
| ^^^^ RET505
208 |
209 | c = 3
|
= help: Remove unnecessary `else`

ℹ Safe fix
204 204 | if x: # [no-else-return]
205 205 | a = 1
206 206 | return y
207 |- else:
208 207 |
209 |- c = 3
210 |- return z
208 |+ c = 3
209 |+ return z


13 changes: 10 additions & 3 deletions crates/ruff_python_trivia/src/textwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,18 @@ pub fn dedent(text: &str) -> Cow<'_, str> {
/// # Panics
/// If the first line is indented by less than the provided indent.
pub fn dedent_to(text: &str, indent: &str) -> String {
// Look at the indentation of the first line, to determine the "baseline" indentation.
// Look at the indentation of the first non-empty line, to determine the "baseline" indentation.
let existing_indent_len = text
.universal_newlines()
.next()
.map_or(0, |line| line.len() - line.trim_start().len());
.find_map(|line| {
let trimmed = line.trim_whitespace_start();
if trimmed.is_empty() {
None
} else {
Some(line.len() - trimmed.len())
}
})
.unwrap_or_default();

// Determine the amount of indentation to remove.
let dedent_len = existing_indent_len - indent.len();
Expand Down
Loading