Skip to content

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

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

Closed
estebank opened this issue Apr 16, 2018 · 3 comments
Closed

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

estebank opened this issue Apr 16, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@estebank
Copy link
Contributor

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 `()`
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Apr 16, 2018
@estebank
Copy link
Contributor Author

Alternative output

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

@ashtneoi
Copy link
Contributor

ashtneoi commented May 18, 2018

I'm not sure if it has the same root cause, but there's a similar issue with break expressions. The code

fn foo() -> usize {
    loop {
        break;
    }
}

gives this error:

error[E0308]: mismatched types
 --> src/main.rs:3:9
  |
3 |         break;
  |         ^^^^^ expected (), found usize
  |
  = note: expected type `()`
             found type `usize`

I assume the error message points at the break expression because it could be changed to break 7; or something, but I don't think that's the first place the average user would consider putting a return value. Mentioning the return value directly would make it easier to tell what's going on.

@lilyball
Copy link
Contributor

This issue is the biggest annoyance I have when using RLS to write Rust code in VSCode, as it causes the entire function to get highlighted as an error, which is very unhelpful. I've started having to adopt a style where I put a dummy statement at the end of the function before writing anything else just to suppress the error while I write the rest of the function, but it's rather annoying to deal with.

bors added a commit that referenced this issue Jan 5, 2019
Modify mismatched type error for functions with no return

Fix #50009.

```
error[E0308]: mismatched types
  --> $DIR/coercion-missing-tail-expected-type.rs:3:24
   |
LL | fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
   |    --------            ^^^ expected i32, found ()
   |    |
   |    this function's body doesn't return
LL |     x + 1;
   |          - help: consider removing this semicolon
   |
   = note: expected type `i32`
              found type `()`
```

instead of

```
error[E0308]: mismatched types
  --> $DIR/coercion-missing-tail-expected-type.rs:3:28
   |
LL |   fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
   |  ____________________________^
LL | |     x + 1;
   | |          - help: consider removing this semicolon
LL | | }
   | |_^ expected i32, found ()
   |
   = note: expected type `i32`
              found type `()`
```
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 C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants