Skip to content

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

Closed
@ChayimFriedman2

Description

@ChayimFriedman2

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.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`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