Skip to content

error help suggests writing down a closure type, which would be a syntax error #91832

Closed
@jendrikw

Description

@jendrikw

Given the following code: play

fn f() {
    vec!['a'].iter().map(|c| c)
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:2:5
  |
2 |     vec!['a'].iter().map(|c| c)
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `Map`
  |
  = note: expected unit type `()`
                found struct `Map<std::slice::Iter<'_, char>, [closure@src/lib.rs:2:26: 2:31]>`
help: consider using a semicolon here
  |
2 |     vec!['a'].iter().map(|c| c);
  |                                +
help: try adding a return type
  |
1 | fn f() -> Map<std::slice::Iter<'_, char>, [closure@src/lib.rs:2:26: 2:31]> {
  |        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Ideally the output should look like:

Don't suggest adding -> Map<std::slice::Iter<'_, char>, [closure@src/lib.rs:2:26: 2:31]> if that's not going to compile.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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