Skip to content

Commit 2b36245

Browse files
authored
Place generated impl block after the existing impl block (#17366)
Placing the generated impl block before the existing one means that the attributes of the existing block will be applied to the newly generated one. Generating the new impl block after the existing one is safer. changelog: [`new_without_default`]: be more robust in the presence of attributes on the impl block containing the `new()` implementation Fixes #17361 (confirmed on original code)
2 parents 6da9f49 + ab45393 commit 2b36245

5 files changed

Lines changed: 190 additions & 105 deletions

File tree

clippy_lints/src/new_without_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
179179
impl_item.span,
180180
format!("you should consider adding a `Default` implementation for `{self_type_snip}`"),
181181
|diag| {
182-
diag.suggest_prepend_item(
182+
diag.suggest_append_item(
183183
cx,
184184
item.span,
185185
"try adding this",

clippy_utils/src/sugg.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -715,19 +715,19 @@ pub trait DiagExt<T: LintContext> {
715715
applicability: Applicability,
716716
);
717717

718-
/// Suggest to add an item before another.
718+
/// Suggest to add an item after another.
719719
///
720720
/// The item should not be indented (except for inner indentation).
721721
///
722722
/// # Example
723723
///
724724
/// ```rust,ignore
725-
/// diag.suggest_prepend_item(cx, item,
725+
/// diag.suggest_append_item(cx, item,
726726
/// "fn foo() {
727727
/// bar();
728728
/// }");
729729
/// ```
730-
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability);
730+
fn suggest_append_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability);
731731

732732
/// Suggest to completely remove an item.
733733
///
@@ -759,24 +759,19 @@ impl<T: LintContext> DiagExt<T> for rustc_errors::Diag<'_, ()> {
759759
}
760760
}
761761

762-
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability) {
762+
fn suggest_append_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability) {
763763
if let Some(indent) = indentation(cx, item) {
764-
let span = item.with_hi(item.lo());
765-
766-
let mut first = true;
767-
let new_item = new_item
768-
.lines()
769-
.map(|l| {
770-
if first {
771-
first = false;
772-
format!("{l}\n")
773-
} else {
774-
format!("{indent}{l}\n")
775-
}
776-
})
777-
.collect::<String>();
778-
779-
self.span_suggestion(span, msg.to_string(), format!("{new_item}\n{indent}"), applicability);
764+
let span = item.shrink_to_hi();
765+
let mut new_item_code = String::new();
766+
for l in new_item.lines() {
767+
writeln!(new_item_code, "{indent}{l}").unwrap();
768+
}
769+
self.span_suggestion(
770+
span,
771+
msg.to_string(),
772+
format!("\n\n{}", new_item_code.strip_suffix('\n').unwrap()),
773+
applicability,
774+
);
780775
}
781776
}
782777

0 commit comments

Comments
 (0)