Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear error message on trailing semicolon in assignment block #44173

Closed
zmanian opened this issue Aug 30, 2017 · 3 comments · Fixed by #106519
Closed

Unclear error message on trailing semicolon in assignment block #44173

zmanian opened this issue Aug 30, 2017 · 3 comments · Fixed by #106519
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zmanian
Copy link
Contributor

zmanian commented Aug 30, 2017

Consider this case

fn main() {

    let x = {
        println!("foo");
        "bar";
    };
    println!("{}",x)
}

this results in the type checking error.

error[E0277]: the trait bound `(): std::fmt::Display` is not satisfied
 --> src/main.rs:8:19
  |
8 |     println!("{}",x)
  |                   ^ `()` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
  |
  = help: the trait `std::fmt::Display` is not implemented for `()`
  = note: required by `std::fmt::Display::fmt`

when the actual error is the trailing semicolon in "bar";

This is similar to #30497 but that handles the case of a function type signature not an assignment.

@Manishearth mentioned it might be easiest to introduce a lint. Looking for mentorship on contributing a fix.

@Manishearth Manishearth added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Aug 30, 2017
@Manishearth
Copy link
Member

The way I see it there are two ways to go around this.

One is to introduce a function that will check if a def is an assignment and check if it's of this kind, and introduce it to every type error that this may affect (a lot of them).

Alternatively, just introduce an early lint that catches all assignments to a block evaluating to (). I don't think there are any good use cases of this outside of macros (which we can macro-check for).

I can mentor this, but AIUI we're pretty strict about adding lints. (@nrc ?)

We could make this a clippy lint but this particular thing is a major newbie stumbling block and should probably be built in.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 30, 2017

We already have this lint in clippy: https://github.com/rust-lang-nursery/rust-clippy/wiki#let_unit_value

So it just needs to be moved to rustc

@Manishearth
Copy link
Member

That lint's not useful here, it's post typecheck.

We need an early lint that checks the AST.

@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 15, 2019
estebank added a commit to estebank/rust that referenced this issue Jan 6, 2023
estebank added a commit to estebank/rust that referenced this issue Jan 6, 2023
@bors bors closed this as completed in a2112fc Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants