Repository: rust-lang/rust (Rust)

Issue:
Missing `use std::fmt::Write;` with `write!`: rustc suggests editing the sysroot
### Code
[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4947b67dcd8b66057486938028542d98)
```rust
fn main() {
    let mut buf = String::new();
    _ = write!(buf, "foo");
}
```

### Current output

```console
Compiling playground v0.0.1 (/playground)
error[E0599]: cannot write into `String`
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    |
   ::: /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    = help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
    |
1   + use std::fmt::Write;
    |
help: there is a method `write_str` with a similar name
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:713:14
    |
713 -         $dst.write_fmt($crate::format_args!($($arg)*))
713 +         $dst.write_str($crate::format_args!($($arg)*))
    |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error
```

### Desired output

```console
Compiling playground v0.0.1 (/playground)
error[E0599]: cannot write into `String`
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    |
   ::: /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    = help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
    |
1   + use std::fmt::Write;
    |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error
```

### Rationale and extra context

Editing libstd is not a solution for a missing trait import

### Rust Version

```console
rustc 1.88.0-nightly (092a284ba 2025-04-13)
binary: rustc
commit-hash: 092a284ba0421695f2032c947765429fd7095796
commit-date: 2025-04-13
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2
```


Discussion: @rustbot label A-suggestion-diagnostics D-invalid-suggestion



Fix (patch):
diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs
index 288d915e85c65..b35aef13c5255 100644
--- a/compiler/rustc_hir_typeck/src/method/suggest.rs
+++ b/compiler/rustc_hir_typeck/src/method/suggest.rs
@@ -1723,8 +1723,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             // Don't emit a suggestion if we found an actual method
             // that had unsatisfied trait bounds
             if unsatisfied_predicates.is_empty()
-                // ...or if we already suggested that name because of `rustc_confusable` annotation.
+                // ...or if we already suggested that name because of `rustc_confusable` annotation
                 && Some(similar_candidate.name()) != confusable_suggested
+                // and if the we aren't in an expansion.
+                && !span.from_expansion()
             {
                 self.find_likely_intended_associated_item(
                     &mut err,