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

Don't suggest 42u8.into(), suggest "change the literal to 42u16 #54160

Closed
estebank opened this issue Sep 12, 2018 · 0 comments
Closed

Don't suggest 42u8.into(), suggest "change the literal to 42u16 #54160

estebank opened this issue Sep 12, 2018 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@estebank
Copy link
Contributor

estebank commented Sep 12, 2018

Given the following code

fn foo(_: u16) {}
fn main() {
    foo(8u8);
}

we currently suggest using .into() to turn the u8 literal into a u16

error[E0308]: mismatched types
 --> src/main.rs:3:9
  |
3 |     foo(8u8);
  |         ^^^ expected u16, found u8
help: you can cast an `u8` to `u16`, which will zero-extend the source value
  |
3 |     foo(8u8.into());
  |         ^^^^^^^^^^

We should suggest changing the literal directly when appropriate in the following way:

error[E0308]: mismatched types
 --> src/main.rs:3:9
  |
3 |     foo(8u8);
  |         ^^^ expected u16, found u8
help: change the type of the numeric literal from `u8` to `u16`
  |
3 |     foo(8u16);
  |         ^^^^

This has the added benefit that we can look at the literal and see if it would also fit when reducing the size (from 16 to 8, for example) if the literal would fit in the smaller type.

The current suggestion is implemented in check_for_cast and would require evaluating the expr to see if it is a literal with an explicit type suffix or not.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 12, 2018
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 18, 2018
Suggest to change numeric literal instead of casting

Closes rust-lang#54160
r? @estebank
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
Projects
None yet
Development

No branches or pull requests

1 participant