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

.into() suggested in const context #49100

Closed
durka opened this issue Mar 17, 2018 · 3 comments
Closed

.into() suggested in const context #49100

durka opened this issue Mar 17, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@durka
Copy link
Contributor

durka commented Mar 17, 2018

const TEN: u8 = 10;
const ALSO_TEN: u16 = TEN;

The compiler suggests .into() instead of an as-cast:

error[E0308]: mismatched types
 --> src/main.rs:2:23
  |
2 | const ALSO_TEN: u16 = TEN;
  |                       ^^^ expected u16, found u8
help: you can cast an `u8` to `u16`, which will zero-extend the source value
  |
2 | const ALSO_TEN: u16 = TEN.into();
  |                       ^^^^^^^^^^

Normally I'm all about phasing out as, and we should somehow make .into() callable on consts, but right now it's suggesting invalid code.

cc @estebank (implemented #47247)

@oli-obk
Copy link
Contributor

oli-obk commented Mar 17, 2018

We have so many cases of these in clippy... we should figure out a way to generalize not producing lints or suggestions in certain contexts

@estebank
Copy link
Contributor

I originally wrote the code to suggest as and then reworked it to use into. In that rework I just have forgotten to think about cases like this. One aware of the problem avoiding this is easy.

As for the generalization, I'd be surprised if we can come up with a way to avoid needing all this context when writing lints, sadly.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 17, 2018

We can ask for the AST parent until we hit an item and then check for const or const fn. But I don't know how to nicely create an API for that

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 19, 2018
kennytm added a commit to kennytm/rust that referenced this issue Mar 20, 2018
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

3 participants