Skip to content

E0308 hint suggests adding a return type to a closure #128561

@lolbinarycat

Description

@lolbinarycat

Code

fn main() {
    b"abc".iter().for_each(|x| dbg!(x))
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:2:32
  |
2 |     b"abc".iter().for_each(|x| dbg!(x))
  |                               -^^^^^^^ expected `()`, found `&u8`
  |                               |
  |                               help: try adding a return type: `-> &u8`
  |

Desired output

error[E0308]: mismatched types
 --> src/main.rs:2:32
  |
2 |     b"abc".iter().for_each(|x| dbg!(x))
  |                               -^^^^^^^ expected `()`, found `&u8`
  |                               |
  |                               help: try ignoring this value: `_ = dbg!(x)`

Rationale and extra context

closures have their return type inferred if not specified, so adding a return type to a closure will never fix a type error.

Other cases

No response

Rust Version

1.80.0

Anything else?

No response

Metadata

Metadata

Assignees

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