Skip to content

rustc shows expanded writeln! macro in code suggestion #112007

Closed
@HayleyDeckers

Description

@HayleyDeckers

Code

fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result {
    if true {
        writeln!(w, "`;?` here ->")?;
    } else {
        writeln!(w, "but not here")
    }
    //intended code was `if [...] { writeln!(...) } else {writeln!(...)}?;`
    Ok(())
}

Current output

error[E0308]: mismatched types
 --> bug_report\src\lib.rs:5:9
  |
2 | /     if true {
3 | |         writeln!(w, "`;?` here ->")?;
4 | |     } else {
5 | |         writeln!(w, "but not here")
  | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`
6 | |     }
  | |_____- expected this to be `()`
  |
  = note: expected unit type `()`
                  found enum `Result<(), std::fmt::Error>`
  = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a semicolon here
  |
6 |     };
  |      +
help: you might have meant to return this value
 --> C:\Users\hayle\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\macros\mod.rs:557:9
  |
55|         return $dst.write_fmt($crate::format_args_nl!($($arg)*));
  |         ++++++                                                  +

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

Desired output

error[E0308]: mismatched types
 --> bug_report\src\lib.rs:5:9
  |
2 | /     if true {
3 | |         writeln!(w, "`;?` here ->")?;
4 | |     } else {
5 | |         writeln!(w, "but not here")
  | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`
6 | |     }
  | |_____- expected this to be `()`
  |
  = note: expected unit type `()`
                  found enum `Result<(), std::fmt::Error>`
  = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a semicolon here
  |
6 |     };
  |      +
help: you might have meant to return this value
 --> bug_report\src\lib.rs:5:9
  |
55|         return writeln!(w, "but not here");
  |         ++++++                            +

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

Rationale and extra context

It might be better still if it suggest adding ?; in the second branch, same as in the first. Or even a single ?; for the whole if-else, but whatever it suggests it probably shouldn't output the macro internals.

Other cases

If you leave off the Ok(()) at the end, it will warn you of the extra ;? in the first branch, as expected, instead.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    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