Skip to content

Mismatched types diagnostic sometimes points at both return type and return statement for the expectation reason #58982

Closed
@estebank

Description

@estebank

The output for the following code is slightly incorrect and shouldn't really point at the value of the return statement:

fn foo(bar: usize) -> usize {
    if bar % 5 == 0 {
        return 1;
    };
}

fn main() {
    println!("Hello, {}!", foo(1))
}
error[E0308]: mismatched types
 --> src/main.rs:1:23
  |
1 | fn foo(bar: usize) -> usize {
  |    ---                ^^^^^ expected usize, found ()
  |    |
  |    this function's body doesn't return
2 |     if bar % 5 == 0 {
3 |         return 1;
  |                - expected because of this statement
  |
  = note: expected type `usize`
             found type `()`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions