Change:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. If you cannot produce a minimal reproduction case (something that would work in isolation), please provide the steps or even link to a repository that causes the problematic output to occur. --Given the following code: The current output is: <details<summaryIf the starting order is reversed, the suggestion puts the macro first in the tree</summary</detailsThis is suggesting to change the code to itself It needs to move the import into a tree; the easy way is to make a new statement (and let rustfmt clean that up); the medium way is to find an existing context; the hard way is when a tree root doesn't already exist (or isn't immediately a tree at that point) determine what the code is using and match that. Related: label +D-confusing +D-incorrect
In this case, the ideal solution is actually to write just and get the import from legacy-lexical-macro-scope into the namespace. I originally hit this with just multiple macros in the use tree, and removing the was the correct fix. I "simplified" for the error report to use a second non-macro item here (to reduce the number of diagnostics). So bonus challenge: handle this case properly and only emit a single help: <details
label +D-invalid-suggestion It's not producing invalid code, but it's surely suggesting a fix which will cause an error (the identical one).

Discussion:
let source_map = self.r.session.source_map(); <ins> // Make sure this is actually crate-relative. let is_definitely_crate = import .module_path .first() .map_or(false, |f| f.ident.name != kw::SelfLower && f.ident.name != kw::Super); </ins> // Add the import to the start, with a `{` if required. let start_point = source_map.start_point(after_crate_name); <del> if let Ok(start_snippet) = source_map.span_to_snippet(start_point) { </del> <ins> if is_definitely_crate && let Ok(start_snippet) = source_map.span_to_snippet(start_point) { </ins> corrections.push(( start_point, if has_nested {

format!("{{{}, {}", import_snippet, start_snippet) }, )); <del> } // Add a `};` to the end if nested, matching the `{` added at the start. if !has_nested { corrections.push((source_map.end_point(after_crate_name), "};".to_string())); </del> <ins> // Add a `};` to the end if nested, matching the `{` added at the start. if !has_nested { corrections.push((source_map.end_point(after_crate_name), "};".to_string())); } } else { // If the root import is module-relative, add the import separately corrections.push(( import.use_span.shrink_to_lo(), format!("use {module_name}::{import_snippet};n"), )); </ins> } }

<ins> // run-rustfix #![allow(unused, nonstandard_style)] mod m { mod p { #[macro_export] macro_rules! nu { {} => {}; } pub struct other_item; } use ::nu; pub use self::p::{other_item as _}; //~^ ERROR unresolved import `self::p::nu` [E0432] //~| HELP a macro with this name exists at the root of the crate } fn main() {} </ins>

<ins> // run-rustfix #![allow(unused, nonstandard_style)] mod m { mod p { #[macro_export] macro_rules! nu { {} => {}; } pub struct other_item; } pub use self::p::{nu, other_item as _}; //~^ ERROR unresolved import `self::p::nu` [E0432] //~| HELP a macro with this name exists at the root of the crate } fn main() {} </ins>

<ins> error[E0432]: unresolved import `self::p::nu` --> $DIR/issue-99695-b.rs:14:23 | LL |     pub use self::p::{nu, other_item as _}; |                       ^^ no `nu` in `m::p` | = note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined help: a macro with this name exists at the root of the crate | LL ~     use ::nu; LL ~ pub use self::p::{other_item as _}; | error: aborting due to previous error For more information about this error, try `rustc --explain E0432`. </ins>

<ins> // run-rustfix #![allow(unused, nonstandard_style)] mod m { #[macro_export] macro_rules! nu { {} => {}; } pub struct other_item; use ::nu; pub use self::{other_item as _}; //~^ ERROR unresolved import `self::nu` [E0432] //~| HELP a macro with this name exists at the root of the crate } fn main() {} </ins>

<ins> // run-rustfix #![allow(unused, nonstandard_style)] mod m { #[macro_export] macro_rules! nu { {} => {}; } pub struct other_item; pub use self::{nu, other_item as _}; //~^ ERROR unresolved import `self::nu` [E0432] //~| HELP a macro with this name exists at the root of the crate } fn main() {} </ins>

<ins> error[E0432]: unresolved import `self::nu` --> $DIR/issue-99695.rs:11:20 | LL |     pub use self::{nu, other_item as _}; |                    ^^ no `nu` in `m` | = note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined help: a macro with this name exists at the root of the crate | LL ~     use ::nu; LL ~ pub use self::{other_item as _}; | error: aborting due to previous error For more information about this error, try `rustc --explain E0432`. </ins>