Skip to content

Commit

Permalink
Stabilize fixes for RET50{5-8} (#12840)
Browse files Browse the repository at this point in the history
Fixes #10099
  • Loading branch information
MichaReiser committed Aug 14, 2024
1 parent 7defc0d commit eb9c7ae
Show file tree
Hide file tree
Showing 13 changed files with 639 additions and 1,103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,11 @@ def f():
return True
else:
return False


def has_untracted_files():
if b'Untracked files' in result.stdout:
return True
else:
\
return False
34 changes: 18 additions & 16 deletions crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,26 +317,28 @@ pub(crate) fn adjust_indentation(
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());
// For simple cases, try to do a manual dedent.
if !contains_multiline_string && !mixed_indentation {
if let Some(dedent) = dedent_to(contents, indentation) {
return Ok(dedent);
}
}

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

let embedding = match_function_def(&mut tree)?;
let mut tree = match_statement(&module_text)?;

let indented_block = match_indented_block(&mut embedding.body)?;
indented_block.indent = Some(indentation);
let embedding = match_function_def(&mut tree)?;

let module_text = indented_block.codegen_stylist(stylist);
let module_text = module_text
.strip_prefix(stylist.line_ending().as_str())
.unwrap()
.to_string();
Ok(module_text)
} else {
// Otherwise, we can do a simple adjustment ourselves.
Ok(dedent_to(contents, indentation))
}
let indented_block = match_indented_block(&mut embedding.body)?;
indented_block.indent = Some(indentation);

let module_text = indented_block.codegen_stylist(stylist);
let module_text = module_text
.strip_prefix(stylist.line_ending().as_str())
.unwrap()
.to_string();
Ok(module_text)
}

/// Determine if a vector contains only one, specific element.
Expand Down
4 changes: 0 additions & 4 deletions crates/ruff_linter/src/rules/flake8_return/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ mod tests {
}

#[test_case(Rule::ImplicitReturn, Path::new("RET503.py"))]
#[test_case(Rule::SuperfluousElseReturn, Path::new("RET505.py"))]
#[test_case(Rule::SuperfluousElseRaise, Path::new("RET506.py"))]
#[test_case(Rule::SuperfluousElseContinue, Path::new("RET507.py"))]
#[test_case(Rule::SuperfluousElseBreak, Path::new("RET508.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
Expand Down
75 changes: 35 additions & 40 deletions crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,14 @@ fn superfluous_else_node(
.unwrap_or_else(|| elif_else.range()),
);
if checker.enabled(diagnostic.kind.rule()) {
if checker.settings.preview.is_enabled() {
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});
}
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});
checker.diagnostics.push(diagnostic);
}
return true;
Expand All @@ -692,16 +690,15 @@ fn superfluous_else_node(
.unwrap_or_else(|| elif_else.range()),
);
if checker.enabled(diagnostic.kind.rule()) {
if checker.settings.preview.is_enabled() {
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});
}
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});

checker.diagnostics.push(diagnostic);
}
return true;
Expand All @@ -712,16 +709,15 @@ fn superfluous_else_node(
.unwrap_or_else(|| elif_else.range()),
);
if checker.enabled(diagnostic.kind.rule()) {
if checker.settings.preview.is_enabled() {
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});
}
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});

checker.diagnostics.push(diagnostic);
}
return true;
Expand All @@ -732,16 +728,15 @@ fn superfluous_else_node(
.unwrap_or_else(|| elif_else.range()),
);
if checker.enabled(diagnostic.kind.rule()) {
if checker.settings.preview.is_enabled() {
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});
}
diagnostic.try_set_fix(|| {
remove_else(
elif_else,
checker.locator(),
checker.indexer(),
checker.stylist(),
)
});

checker.diagnostics.push(diagnostic);
}
return true;
Expand Down
Loading

0 comments on commit eb9c7ae

Please sign in to comment.