mirror of https://github.com/astral-sh/ruff
[`ruff_linter`] - Use LibCST in `adjust_indentation` for mixed whitespace (#12740)
This commit is contained in:
parent
df7345e118
commit
6d9205e346
|
|
@ -238,3 +238,9 @@ def indent(x, y, w, z):
|
||||||
# comment
|
# comment
|
||||||
c = 3
|
c = 3
|
||||||
return z
|
return z
|
||||||
|
|
||||||
|
def f():
|
||||||
|
if True:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
|
||||||
|
|
@ -300,11 +300,25 @@ pub(crate) fn adjust_indentation(
|
||||||
indexer: &Indexer,
|
indexer: &Indexer,
|
||||||
stylist: &Stylist,
|
stylist: &Stylist,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
|
let contents = locator.slice(range);
|
||||||
|
|
||||||
// If the range includes a multi-line string, use LibCST to ensure that we don't adjust the
|
// If the range includes a multi-line string, use LibCST to ensure that we don't adjust the
|
||||||
// whitespace _within_ the string.
|
// whitespace _within_ the string.
|
||||||
if indexer.multiline_ranges().intersects(range) || indexer.fstring_ranges().intersects(range) {
|
let contains_multiline_string =
|
||||||
let contents = locator.slice(range);
|
indexer.multiline_ranges().intersects(range) || indexer.fstring_ranges().intersects(range);
|
||||||
|
|
||||||
|
// If the range has mixed indentation, we will use LibCST as well.
|
||||||
|
let mixed_indentation = contents.universal_newlines().any(|line| {
|
||||||
|
let trimmed = line.trim_whitespace_start();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let line_indentation: &str = &line[..line.len() - trimmed.len()];
|
||||||
|
line_indentation.contains('\t') && line_indentation.contains(' ')
|
||||||
|
});
|
||||||
|
|
||||||
|
if contains_multiline_string || mixed_indentation {
|
||||||
let module_text = format!("def f():{}{contents}", stylist.line_ending().as_str());
|
let module_text = format!("def f():{}{contents}", stylist.line_ending().as_str());
|
||||||
|
|
||||||
let mut tree = match_statement(&module_text)?;
|
let mut tree = match_statement(&module_text)?;
|
||||||
|
|
@ -322,7 +336,6 @@ pub(crate) fn adjust_indentation(
|
||||||
Ok(module_text)
|
Ok(module_text)
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, we can do a simple adjustment ourselves.
|
// Otherwise, we can do a simple adjustment ourselves.
|
||||||
let contents = locator.slice(range);
|
|
||||||
Ok(dedent_to(contents, indentation))
|
Ok(dedent_to(contents, indentation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -215,4 +215,12 @@ RET505.py:237:5: RET505 Unnecessary `else` after `return` statement
|
||||||
|
|
|
|
||||||
= help: Remove unnecessary `else`
|
= help: Remove unnecessary `else`
|
||||||
|
|
||||||
|
RET505.py:245:2: RET505 Unnecessary `else` after `return` statement
|
||||||
|
|
|
||||||
|
243 | if True:
|
||||||
|
244 | return True
|
||||||
|
245 | else:
|
||||||
|
| ^^^^ RET505
|
||||||
|
246 | return False
|
||||||
|
|
|
||||||
|
= help: Remove unnecessary `else`
|
||||||
|
|
|
||||||
|
|
@ -460,5 +460,24 @@ RET505.py:237:5: RET505 [*] Unnecessary `else` after `return` statement
|
||||||
240 |- return z
|
240 |- return z
|
||||||
238 |+ c = 3
|
238 |+ c = 3
|
||||||
239 |+ return z
|
239 |+ return z
|
||||||
|
241 240 |
|
||||||
|
242 241 | def f():
|
||||||
|
243 242 | if True:
|
||||||
|
|
||||||
|
RET505.py:245:2: RET505 [*] Unnecessary `else` after `return` statement
|
||||||
|
|
|
||||||
|
243 | if True:
|
||||||
|
244 | return True
|
||||||
|
245 | else:
|
||||||
|
| ^^^^ RET505
|
||||||
|
246 | return False
|
||||||
|
|
|
||||||
|
= help: Remove unnecessary `else`
|
||||||
|
|
||||||
|
ℹ Safe fix
|
||||||
|
242 242 | def f():
|
||||||
|
243 243 | if True:
|
||||||
|
244 244 | return True
|
||||||
|
245 |- else:
|
||||||
|
246 |- return False
|
||||||
|
245 |+ return False
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue