Skip to content

Consider removing a non-existing semicolon #81098

Closed
@adlerd

Description

@adlerd

I tried this code:

fn wat() -> impl core::fmt::Display {
    fn why() {}
}

I expected to see this happen: An error explaining that () does not impl Display (although a different message would be nice, cf #54771).

Instead, this happened: I got that error, but I also got a suggestion to remove a semicolon. As you can see, this program does not contain any semicolons to remove.

error[E0277]: `()` doesn't implement `std::fmt::Display`
 --> src/lib.rs:1:13
  |
1 | fn wat() -> impl core::fmt::Display {
  |             ^^^^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
2 |     fn why() {}
  |               - consider removing this semicolon
  |
  = help: the trait `std::fmt::Display` is not implemented for `()`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-unknown-linux-gnu
release: 1.49.0

Of course, I understand why the suggestion exists, but rustc should probably check to see if a semicolon exists before suggesting to remove it. For bonus points, rustc should find the semicolon correctly in this slightly modified program. (Currently it still points at why's }.)

fn wat() -> impl core::fmt::Display {
    5i32;
    fn why() {}
}

Here is perhaps a worse variant of the same issue:

fn wat() -> impl core::fmt::Display {
    5i32;
    const WHY: () = ();
}

Here the suggestion is to remove the semicolon after the WHY item, which is now a "syntactically correct suggestion" so to speak (i.e., the "remove a semicolon" is now pointing at an actual semicolon)... but applying it does not result in a syntactically correct program.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-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