Skip to content

"Consider adding a semicolon here" for a closure without block #97359

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

Closed
ChayimFriedman2 opened this issue May 24, 2022 · 0 comments · Fixed by #97371
Closed

"Consider adding a semicolon here" for a closure without block #97359

ChayimFriedman2 opened this issue May 24, 2022 · 0 comments · Fixed by #97371
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented May 24, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=cc081036ff00b0844aac10d9fdac636f

fn foo(_f: impl Fn()) {}

fn bar() -> i32 { 1 }

fn main() {
    foo(|| bar())
}

The current output is:

error[E0308]: mismatched types
 --> src/main.rs:6:12
  |
5 | fn main() {
  |           - expected `()` because of default return type
6 |     foo(|| bar())
  |            ^^^^^ expected `()`, found `i32`

Ideally rustc should suggest adding a semicolon (and a block):

error[E0308]: mismatched types
 --> src/main.rs:6:14
  |
6 |     foo(|| { bar(); })
  |            ++     +++- help: consider using a semicolon here
  |              |
  |              expected `()`, found `i32`

It already does that if we have a block https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=433c9f8d9638e927fe4f5ba66d5551fa:

foo(|| { bar() })
error[E0308]: mismatched types
 --> src/main.rs:6:14
  |
6 |     foo(|| { bar() })
  |              ^^^^^- help: consider using a semicolon here: `;`
  |              |
  |              expected `()`, found `i32`

@rustbot label +A-suggestion-diagnostics

@rustbot claim

Noticed by this StackOverflow question.

@ChayimFriedman2 ChayimFriedman2 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 24, 2022
@rustbot rustbot added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label May 24, 2022
@bors bors closed this as completed in 38d8167 May 31, 2022
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.

2 participants