Skip to content

Commit deb6159

Browse files
committed
move snippet creation into span_lint_hir_and_then
made a separate commit just to simplify the diff
1 parent 193c725 commit deb6159

1 file changed

Lines changed: 69 additions & 52 deletions

File tree

clippy_lints/src/new_vs_default.rs

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -217,58 +217,6 @@ impl<'tcx> LateLintPass<'tcx> for NewVsDefault {
217217
return;
218218
}
219219

220-
let mut app = Applicability::MachineApplicable;
221-
let attrs_sugg = {
222-
let mut sugg = String::new();
223-
for attr in cx.tcx.hir_attrs(assoc_item_hir_id) {
224-
let Attribute::Parsed(AttributeKind::CfgTrace(attrs)) = attr else {
225-
// This might be some other attribute that the `impl Default` ought to inherit.
226-
// But it could also be one of the many attributes that:
227-
// - can't be put on an impl block -- like `#[inline]`
228-
// - we can't even build a suggestion for, since `Attribute::span` may panic.
229-
//
230-
// Because of all that, remain on the safer side -- don't inherit this attr, and
231-
// just reduce the applicability
232-
app = Applicability::MaybeIncorrect;
233-
continue;
234-
};
235-
236-
for (_, attr_span) in attrs {
237-
sugg.push_str(&snippet_with_applicability(cx.sess(), *attr_span, "_", &mut app));
238-
sugg.push('\n');
239-
}
240-
}
241-
sugg
242-
};
243-
let generics_sugg = snippet_with_applicability(cx, generics.span, "", &mut app);
244-
let where_clause_sugg = if generics.has_where_clause_predicates {
245-
let where_clause_sugg =
246-
snippet_with_applicability(cx, generics.where_clause_span, "", &mut app).to_string();
247-
let mut where_clause_sugg = reindent_multiline(&where_clause_sugg, true, Some(4));
248-
if impl_item.generics.has_where_clause_predicates {
249-
if !where_clause_sugg.ends_with(',') {
250-
where_clause_sugg.push(',');
251-
}
252-
253-
let additional_where_preds =
254-
snippet_with_applicability(cx, impl_item.generics.where_clause_span, "", &mut app);
255-
let indent = indent_of(cx, generics.where_clause_span).unwrap_or(0);
256-
// Remove the leading `where ` keyword
257-
let additional_where_preds =
258-
additional_where_preds.trim_start_matches("where").trim_start();
259-
where_clause_sugg.push('\n');
260-
where_clause_sugg.extend(std::iter::repeat_n(' ', indent));
261-
where_clause_sugg.push_str(additional_where_preds);
262-
}
263-
format!("\n{where_clause_sugg}\n")
264-
} else if impl_item.generics.has_where_clause_predicates {
265-
let where_clause_sugg =
266-
snippet_with_applicability(cx, impl_item.generics.where_clause_span, "", &mut app);
267-
let where_clause_sugg = reindent_multiline(&where_clause_sugg, true, Some(4));
268-
format!("\n{}\n", where_clause_sugg.trim_start())
269-
} else {
270-
String::new()
271-
};
272220
let self_type_snip = snippet_opt(cx, impl_self_ty.span).unwrap_or_else(|| self_ty.to_string());
273221
span_lint_hir_and_then(
274222
cx,
@@ -277,6 +225,75 @@ impl<'tcx> LateLintPass<'tcx> for NewVsDefault {
277225
impl_item.span,
278226
format!("you should consider adding a `Default` implementation for `{self_type_snip}`"),
279227
|diag| {
228+
let mut app = Applicability::MachineApplicable;
229+
let attrs_sugg = {
230+
let mut sugg = String::new();
231+
for attr in cx.tcx.hir_attrs(assoc_item_hir_id) {
232+
let Attribute::Parsed(AttributeKind::CfgTrace(attrs)) = attr else {
233+
// This might be some other attribute that the `impl Default` ought to
234+
// inherit. But it could also be
235+
// one of the many attributes that:
236+
// - can't be put on an impl block -- like `#[inline]`
237+
// - we can't even build a suggestion for, since `Attribute::span` may
238+
// panic.
239+
//
240+
// Because of all that, remain on the safer side -- don't inherit this attr,
241+
// and just reduce the
242+
// applicability
243+
app = Applicability::MaybeIncorrect;
244+
continue;
245+
};
246+
247+
for (_, attr_span) in attrs {
248+
sugg.push_str(&snippet_with_applicability(
249+
cx.sess(),
250+
*attr_span,
251+
"_",
252+
&mut app,
253+
));
254+
sugg.push('\n');
255+
}
256+
}
257+
sugg
258+
};
259+
let generics_sugg = snippet_with_applicability(cx, generics.span, "", &mut app);
260+
let where_clause_sugg = if generics.has_where_clause_predicates {
261+
let where_clause_sugg =
262+
snippet_with_applicability(cx, generics.where_clause_span, "", &mut app)
263+
.to_string();
264+
let mut where_clause_sugg = reindent_multiline(&where_clause_sugg, true, Some(4));
265+
if impl_item.generics.has_where_clause_predicates {
266+
if !where_clause_sugg.ends_with(',') {
267+
where_clause_sugg.push(',');
268+
}
269+
270+
let additional_where_preds = snippet_with_applicability(
271+
cx,
272+
impl_item.generics.where_clause_span,
273+
"",
274+
&mut app,
275+
);
276+
let indent = indent_of(cx, generics.where_clause_span).unwrap_or(0);
277+
// Remove the leading `where ` keyword
278+
let additional_where_preds =
279+
additional_where_preds.trim_start_matches("where").trim_start();
280+
where_clause_sugg.push('\n');
281+
where_clause_sugg.extend(std::iter::repeat_n(' ', indent));
282+
where_clause_sugg.push_str(additional_where_preds);
283+
}
284+
format!("\n{where_clause_sugg}\n")
285+
} else if impl_item.generics.has_where_clause_predicates {
286+
let where_clause_sugg = snippet_with_applicability(
287+
cx,
288+
impl_item.generics.where_clause_span,
289+
"",
290+
&mut app,
291+
);
292+
let where_clause_sugg = reindent_multiline(&where_clause_sugg, true, Some(4));
293+
format!("\n{}\n", where_clause_sugg.trim_start())
294+
} else {
295+
String::new()
296+
};
280297
diag.suggest_prepend_item(
281298
cx,
282299
item.span,

0 commit comments

Comments
 (0)