Skip to content

Diagnostic for forgetting a writer in write! is bad #108713

Closed
@mejrs

Description

@mejrs

Code

fn main() {
    let x = 1;
    let y = 2;
    write!("{}_{}", x, y);
}

Current output

Compiling playground v0.0.1 (/playground)
error: format argument must be a string literal
 --> src/main.rs:4:21
  |
4 |     write!("{}_{}", x, y);
  |                     ^
  |
help: you might be missing a string literal to format with
  |
4 |     write!("{}_{}", "{} {}", x, y);
  |                     ++++++++

error[E0599]: no method named `write_fmt` found for reference `&'static str` in the current scope
 --> src/main.rs:4:5
  |
4 |     write!("{}_{}", x, y);
  |     ^^^^^^^^^^^^^^^^^^^^^ method not found in `&str`
  |
  = note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to 2 previous errors

Desired output

error: cannot write into `&'static str`
 --> src/main.rs:4:21
  |
4 |     write!("{}_{}", x, y);
  |             ^^^^
  |
help: you might be missing a writer to write into
  +    let mut writer = String::new();
  |
4 |     write!(&mut writer, "{}_{}", x, y);
  |            ++++++++++++

Rationale and extra context

No response

Other cases

No response

Anything else?

It could also recommend using format!

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions