Skip to content

On functions with return type and no returned value, point at appropriate bindings to be returned #98177

Closed
@estebank

Description

@estebank

When encountering a function with an explicit return type, but the function body doesn't return anything, we should inspect the body's bindings and point out all that have an appropriate type:

fn foo() -> Option<i32> {
    let x: Option<i32> = Some(42);
}
error[E0308]: mismatched types
 --> src/lib.rs:1:13
  |
1 | fn foo() -> Option<i32> {
  |    ---      ^^^^^^^^^^^ expected enum `Option`, found `()`
  |    |
  |    implicitly returns `()` as its body has no tail or `return` expression
2 |     let x = Some(42);
  |         - this binding is of the expected return type, you might have meant to `return` it
  |
  = note:   expected enum `Option<i32>`
          found unit type `()`

Potentially consider the implications of lifetimes, particularly for bindings that are a borrowed value. Also make an effort not to drown the output if more than N bindings would apply in really big functions.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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