diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 8ab0087ae6158..efa6e08a34fa4 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -486,6 +486,9 @@ impl Resolver<'_, '_> { let remove_whole_use = remove_spans.len() == 1 && remove_spans[0] == unused.item_span; let num_to_remove = ms.primary_spans().len(); + // Only offer rustfix suggestions for spans that point at directly editable code. + let can_suggest_removal = + remove_spans.iter().all(|span| span.can_be_used_for_suggestions()); // If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]` // attribute; however, if not, suggest adding the attribute. There is no way to @@ -516,11 +519,13 @@ impl Resolver<'_, '_> { unused.use_tree_id, ms, move |dcx, level, sess| { - let sugg = if remove_whole_use { - errors::UnusedImportsSugg::RemoveWholeUse { span: remove_spans[0] } - } else { - errors::UnusedImportsSugg::RemoveImports { remove_spans, num_to_remove } - }; + let sugg = can_suggest_removal.then(|| { + if remove_whole_use { + errors::UnusedImportsSugg::RemoveWholeUse { span: remove_spans[0] } + } else { + errors::UnusedImportsSugg::RemoveImports { remove_spans, num_to_remove } + } + }); let test_module_span = test_module_span.map(|span| { sess.downcast_ref::() .expect("expected a `Session`") diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 342484a8c0d51..4bcc7a5eb02e8 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -1742,7 +1742,7 @@ pub(crate) struct ElidedLifetimesInPaths { )] pub(crate) struct UnusedImports { #[subdiagnostic] - pub sugg: UnusedImportsSugg, + pub sugg: Option, #[help("if this is a test module, consider adding a `#[cfg(test)]` to the containing module")] pub test_module_span: Option, diff --git a/tests/ui/imports/unused-import-in-macro-expansion-rustfix.fixed b/tests/ui/imports/unused-import-in-macro-expansion-rustfix.fixed new file mode 100644 index 0000000000000..3b7c8051122fc --- /dev/null +++ b/tests/ui/imports/unused-import-in-macro-expansion-rustfix.fixed @@ -0,0 +1,23 @@ +// Regression test for +//@ edition:2021 +//@ run-rustfix +//@ rustfix-only-machine-applicable +//@ check-pass + +mod m { + macro_rules! define_new_macro { + ($name:ident) => { + macro_rules! $name { + () => {}; + } + pub(crate) use $name; + }; + } + + define_new_macro!(item_used); + define_new_macro!(item_unused); +} + +fn main() { + m::item_used!(); +} diff --git a/tests/ui/imports/unused-import-in-macro-expansion-rustfix.rs b/tests/ui/imports/unused-import-in-macro-expansion-rustfix.rs new file mode 100644 index 0000000000000..3b7c8051122fc --- /dev/null +++ b/tests/ui/imports/unused-import-in-macro-expansion-rustfix.rs @@ -0,0 +1,23 @@ +// Regression test for +//@ edition:2021 +//@ run-rustfix +//@ rustfix-only-machine-applicable +//@ check-pass + +mod m { + macro_rules! define_new_macro { + ($name:ident) => { + macro_rules! $name { + () => {}; + } + pub(crate) use $name; + }; + } + + define_new_macro!(item_used); + define_new_macro!(item_unused); +} + +fn main() { + m::item_used!(); +}