Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
estebank opened this issue Jun 16, 2022 · 1 comment · Fixed by #98784
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jun 16, 2022

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.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Jun 16, 2022
@compiler-errors
Copy link
Member

This seems like a fun one. Maybe I'll give it a stab.

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants