Skip to content

Commit d43a2db

Browse files
committed
autofix for significant_drop_in_scrutinee
1 parent 396de57 commit d43a2db

6 files changed

+980
-73
lines changed

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ fn check<'tcx>(
104104
let mut helper = SigDropHelper::new(cx);
105105
let suggestions = helper.find_sig_drop(scrutinee);
106106

107-
for found in suggestions {
107+
for (i, found) in suggestions.iter().enumerate() {
108108
span_lint_and_then(cx, SIGNIFICANT_DROP_IN_SCRUTINEE, found.found_span, message, |diag| {
109109
match sugg {
110-
Suggestion::Emit => set_suggestion(diag, cx, expr, found),
110+
Suggestion::Emit => set_suggestion(diag, cx, expr, *found, suggestions.len(), i),
111111
Suggestion::DontEmit => (),
112112
}
113113

@@ -121,15 +121,27 @@ fn check<'tcx>(
121121
}
122122
}
123123

124-
fn set_suggestion<'tcx>(diag: &mut Diag<'_, ()>, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, found: FoundSigDrop) {
124+
fn set_suggestion<'tcx>(
125+
diag: &mut Diag<'_, ()>,
126+
cx: &LateContext<'tcx>,
127+
expr: &'tcx Expr<'tcx>,
128+
found: FoundSigDrop,
129+
total_sugg: usize,
130+
curr_sugg_idx: usize,
131+
) {
125132
let original = snippet(cx, found.found_span, "..");
126133
let trailing_indent = " ".repeat(indent_of(cx, found.found_span).unwrap_or(0));
127134

128135
let replacement = {
129136
let (def_part, deref_part) = if found.is_unit_return_val {
130-
("", String::new())
137+
(String::new(), String::new())
138+
} else if total_sugg == 1 {
139+
("let value = ".to_string(), "*".repeat(found.peel_ref_times))
131140
} else {
132-
("let value = ", "*".repeat(found.peel_ref_times))
141+
(
142+
format!("let value{} = ", curr_sugg_idx + 1),
143+
"*".repeat(found.peel_ref_times),
144+
)
133145
};
134146
format!("{def_part}{deref_part}{original};\n{trailing_indent}")
135147
};
@@ -143,7 +155,11 @@ fn set_suggestion<'tcx>(diag: &mut Diag<'_, ()>, cx: &LateContext<'tcx>, expr: &
143155
let scrutinee_replacement = if found.is_unit_return_val {
144156
"()".to_owned()
145157
} else if found.peel_ref_times == 0 {
146-
"value".to_owned()
158+
if total_sugg == 1 {
159+
"value".to_owned()
160+
} else {
161+
format!("value{}", curr_sugg_idx + 1)
162+
}
147163
} else {
148164
let ref_part = "&".repeat(found.peel_ref_times);
149165
format!("({ref_part}value)")
@@ -155,7 +171,7 @@ fn set_suggestion<'tcx>(diag: &mut Diag<'_, ()>, cx: &LateContext<'tcx>, expr: &
155171
(first_line_of_span(cx, expr.span).shrink_to_lo(), replacement),
156172
(found.found_span, scrutinee_replacement),
157173
],
158-
Applicability::MaybeIncorrect,
174+
Applicability::MachineApplicable,
159175
);
160176
}
161177

0 commit comments

Comments
 (0)