Skip to content

Improve output of type mismatch due to lack of return value #50009

Closed
@estebank

Description

@estebank

When you have

fn foo() -> usize {
    ""
}

the diagnostic correctly points at the return value

error[E0308]: mismatched types
 --> src/main.rs:2:5
  |
1 | fn foo() -> usize {
  |             ----- expected `usize` because of return type
2 |     ""
  |     ^^ expected usize, found reference
  |
  = note: expected type `usize`
             found type `&'static str`

while if the error is due to not having a return value

fn bar() -> usize {
    3;
}

the output is

error[E0308]: mismatched types
 --> src/main.rs:5:19
  |
5 |   fn bar() -> usize {
  |  ___________________^
6 | |     3;
  | |      - help: consider removing this semicolon
7 | | }
  | |_^ expected usize, found ()
  |
  = note: expected type `usize`
             found type `()`

It probably should be

error[E0308]: mismatched types
 --> src/main.rs:5:19
  |
5 |   fn bar() -> usize {
  |               ----- expected due to this return type
6 |     3;
  |     ^- help: consider removing this semicolon
  |     |
  |     expected usize, found ()
  |
  = note: expected type `usize`
             found type `()`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions