Skip to content

format! .into() suggestion deletes the format macro #110017

Closed

Description

Code

pub fn foo(x: &str) -> Result<(), Box<dyn std::error::Error>> {
    Err(format!("error: {x}"))
}

Current output

Checking foo v0.1.0 (/Users/eric/Temp/foo)
error[E0308]: mismatched types
 --> src/lib.rs:2:9
  |
2 |     Err(format!("error: {x}"))
  |         ^^^^^^^^^^^^^^^^^^^^^ expected `Box<dyn Error>`, found `String`
  |
  = note: expected struct `Box<dyn std::error::Error>`
             found struct `String`
  = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
help: call `Into::into` on this expression to convert `String` into `Box<dyn std::error::Error>`
 --> /Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/macros.rs:121:12
  |
12|         res.into()
  |            +++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `foo` (lib) due to previous error

Desired output

Checking foo v0.1.0 (/Users/eric/Temp/foo)
error[E0308]: mismatched types
 --> src/lib.rs:2:9
  |
2 |     Err(format!("error: {x}"))
  |         ^^^^^^^^^^^^^^^^^^^^^ expected `Box<dyn Error>`, found `String`
  |
  = note: expected struct `Box<dyn std::error::Error>`
             found struct `String`
  = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
help: call `Into::into` on this expression to convert `String` into `Box<dyn std::error::Error>`
 --> /Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/macros.rs:121:12
  |
12|          Err(format!("error: {x}").into())
  |                                   +++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `foo` (lib) due to previous error

Rationale and extra context

The suggestion converts the code to:

Err(.into())

which is invalid.

It should suggest:

Err(format!("error: {x}").into())

The suggestion works correctly for non-macro string values.

This suggestion was introduced in #102496.

Other cases

No response

Anything else?

rustc 1.70.0-nightly (cf7ada217 2023-04-03)
binary: rustc
commit-hash: cf7ada217c8ac63367b184afd9fffaff30f6ed44
commit-date: 2023-04-03
host: aarch64-apple-darwin
release: 1.70.0-nightly
LLVM version: 16.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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