Skip to content

Commit eb9c7ae

Browse files
committed
Stabilize fixes for RET50{5-8} (#12840)
Fixes #10099
1 parent 7defc0d commit eb9c7ae

13 files changed

+639
-1103
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_return/RET505.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,11 @@ def f():
244244
return True
245245
else:
246246
return False
247+
248+
249+
def has_untracted_files():
250+
if b'Untracked files' in result.stdout:
251+
return True
252+
else:
253+
\
254+
return False

crates/ruff_linter/src/fix/edits.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,28 @@ pub(crate) fn adjust_indentation(
317317
line_indentation.contains('\t') && line_indentation.contains(' ')
318318
});
319319

320-
if contains_multiline_string || mixed_indentation {
321-
let module_text = format!("def f():{}{contents}", stylist.line_ending().as_str());
320+
// For simple cases, try to do a manual dedent.
321+
if !contains_multiline_string && !mixed_indentation {
322+
if let Some(dedent) = dedent_to(contents, indentation) {
323+
return Ok(dedent);
324+
}
325+
}
322326

323-
let mut tree = match_statement(&module_text)?;
327+
let module_text = format!("def f():{}{contents}", stylist.line_ending().as_str());
324328

325-
let embedding = match_function_def(&mut tree)?;
329+
let mut tree = match_statement(&module_text)?;
326330

327-
let indented_block = match_indented_block(&mut embedding.body)?;
328-
indented_block.indent = Some(indentation);
331+
let embedding = match_function_def(&mut tree)?;
329332

330-
let module_text = indented_block.codegen_stylist(stylist);
331-
let module_text = module_text
332-
.strip_prefix(stylist.line_ending().as_str())
333-
.unwrap()
334-
.to_string();
335-
Ok(module_text)
336-
} else {
337-
// Otherwise, we can do a simple adjustment ourselves.
338-
Ok(dedent_to(contents, indentation))
339-
}
333+
let indented_block = match_indented_block(&mut embedding.body)?;
334+
indented_block.indent = Some(indentation);
335+
336+
let module_text = indented_block.codegen_stylist(stylist);
337+
let module_text = module_text
338+
.strip_prefix(stylist.line_ending().as_str())
339+
.unwrap()
340+
.to_string();
341+
Ok(module_text)
340342
}
341343

342344
/// Determine if a vector contains only one, specific element.

crates/ruff_linter/src/rules/flake8_return/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ mod tests {
3636
}
3737

3838
#[test_case(Rule::ImplicitReturn, Path::new("RET503.py"))]
39-
#[test_case(Rule::SuperfluousElseReturn, Path::new("RET505.py"))]
40-
#[test_case(Rule::SuperfluousElseRaise, Path::new("RET506.py"))]
41-
#[test_case(Rule::SuperfluousElseContinue, Path::new("RET507.py"))]
42-
#[test_case(Rule::SuperfluousElseBreak, Path::new("RET508.py"))]
4339
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
4440
let snapshot = format!(
4541
"preview__{}_{}",

crates/ruff_linter/src/rules/flake8_return/rules/function.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -672,16 +672,14 @@ fn superfluous_else_node(
672672
.unwrap_or_else(|| elif_else.range()),
673673
);
674674
if checker.enabled(diagnostic.kind.rule()) {
675-
if checker.settings.preview.is_enabled() {
676-
diagnostic.try_set_fix(|| {
677-
remove_else(
678-
elif_else,
679-
checker.locator(),
680-
checker.indexer(),
681-
checker.stylist(),
682-
)
683-
});
684-
}
675+
diagnostic.try_set_fix(|| {
676+
remove_else(
677+
elif_else,
678+
checker.locator(),
679+
checker.indexer(),
680+
checker.stylist(),
681+
)
682+
});
685683
checker.diagnostics.push(diagnostic);
686684
}
687685
return true;
@@ -692,16 +690,15 @@ fn superfluous_else_node(
692690
.unwrap_or_else(|| elif_else.range()),
693691
);
694692
if checker.enabled(diagnostic.kind.rule()) {
695-
if checker.settings.preview.is_enabled() {
696-
diagnostic.try_set_fix(|| {
697-
remove_else(
698-
elif_else,
699-
checker.locator(),
700-
checker.indexer(),
701-
checker.stylist(),
702-
)
703-
});
704-
}
693+
diagnostic.try_set_fix(|| {
694+
remove_else(
695+
elif_else,
696+
checker.locator(),
697+
checker.indexer(),
698+
checker.stylist(),
699+
)
700+
});
701+
705702
checker.diagnostics.push(diagnostic);
706703
}
707704
return true;
@@ -712,16 +709,15 @@ fn superfluous_else_node(
712709
.unwrap_or_else(|| elif_else.range()),
713710
);
714711
if checker.enabled(diagnostic.kind.rule()) {
715-
if checker.settings.preview.is_enabled() {
716-
diagnostic.try_set_fix(|| {
717-
remove_else(
718-
elif_else,
719-
checker.locator(),
720-
checker.indexer(),
721-
checker.stylist(),
722-
)
723-
});
724-
}
712+
diagnostic.try_set_fix(|| {
713+
remove_else(
714+
elif_else,
715+
checker.locator(),
716+
checker.indexer(),
717+
checker.stylist(),
718+
)
719+
});
720+
725721
checker.diagnostics.push(diagnostic);
726722
}
727723
return true;
@@ -732,16 +728,15 @@ fn superfluous_else_node(
732728
.unwrap_or_else(|| elif_else.range()),
733729
);
734730
if checker.enabled(diagnostic.kind.rule()) {
735-
if checker.settings.preview.is_enabled() {
736-
diagnostic.try_set_fix(|| {
737-
remove_else(
738-
elif_else,
739-
checker.locator(),
740-
checker.indexer(),
741-
checker.stylist(),
742-
)
743-
});
744-
}
731+
diagnostic.try_set_fix(|| {
732+
remove_else(
733+
elif_else,
734+
checker.locator(),
735+
checker.indexer(),
736+
checker.stylist(),
737+
)
738+
});
739+
745740
checker.diagnostics.push(diagnostic);
746741
}
747742
return true;

0 commit comments

Comments
 (0)