rust-lang / rust-lang/rust

E0308 hint suggests adding a return type to a closure

Open
#128,561 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 mismatch error.

Other cases

No response

Rust Version
1.80.0
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the E0308 diagnostic with the closure example on Rust 1.80.0, then trace the compiler diagnostic that suggests adding a closure return type. Done means this case suggests ignoring the value with _ = dbg!(x) instead, without changing unrelated diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.