Skip to content

Commit

Permalink
autofix for significant_drop_in_scrutinee
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Jan 24, 2025
1 parent 396de57 commit 9edf1e7
Show file tree
Hide file tree
Showing 6 changed files with 979 additions and 72 deletions.
28 changes: 22 additions & 6 deletions clippy_lints/src/matches/significant_drop_in_scrutinee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ fn check<'tcx>(
let mut helper = SigDropHelper::new(cx);
let suggestions = helper.find_sig_drop(scrutinee);

for found in suggestions {
for (i, found) in suggestions.iter().enumerate() {
span_lint_and_then(cx, SIGNIFICANT_DROP_IN_SCRUTINEE, found.found_span, message, |diag| {
match sugg {
Suggestion::Emit => set_suggestion(diag, cx, expr, found),
Suggestion::Emit => set_suggestion(diag, cx, expr, *found, suggestions.len(), i),
Suggestion::DontEmit => (),
}

Expand All @@ -121,15 +121,27 @@ fn check<'tcx>(
}
}

fn set_suggestion<'tcx>(diag: &mut Diag<'_, ()>, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, found: FoundSigDrop) {
fn set_suggestion<'tcx>(
diag: &mut Diag<'_, ()>,
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'tcx>,
found: FoundSigDrop,
total_sugg: usize,
curr_sugg_idx: usize,
) {
let original = snippet(cx, found.found_span, "..");
let trailing_indent = " ".repeat(indent_of(cx, found.found_span).unwrap_or(0));

let replacement = {
let (def_part, deref_part) = if found.is_unit_return_val {
("", String::new())
(String::new(), String::new())
} else if total_sugg == 1 {
("let value = ".to_string(), "*".repeat(found.peel_ref_times))
} else {
("let value = ", "*".repeat(found.peel_ref_times))
(
format!("let value{} = ", curr_sugg_idx + 1),
"*".repeat(found.peel_ref_times),
)
};
format!("{def_part}{deref_part}{original};\n{trailing_indent}")
};
Expand All @@ -143,7 +155,11 @@ fn set_suggestion<'tcx>(diag: &mut Diag<'_, ()>, cx: &LateContext<'tcx>, expr: &
let scrutinee_replacement = if found.is_unit_return_val {
"()".to_owned()
} else if found.peel_ref_times == 0 {
"value".to_owned()
if total_sugg == 1 {
"value".to_owned()
} else {
format!("value{}", curr_sugg_idx + 1)
}
} else {
let ref_part = "&".repeat(found.peel_ref_times);
format!("({ref_part}value)")
Expand Down
Loading

0 comments on commit 9edf1e7

Please sign in to comment.