fix: more descriptive error message for enum to integer#151122
fix: more descriptive error message for enum to integer#151122Jaidenmagnan wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
r? @chenyukang rustbot has assigned @chenyukang. Use |
|
|
||
| fn main() { | ||
| let priority = &Priority::Normal; | ||
| let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid |
There was a problem hiding this comment.
It might be a good idea to check the HELP message as well.
like
| let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid | |
| let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid | |
| //~| HELP: dereference the expression |
There was a problem hiding this comment.
It might be a good idea to check the HELP message as well. like
Ty! will add this.
|
you can use there is no need to paste a screenshot of CI testing, since if any tests in CI failed, we will get it in Github action . |
ty, just fixed this. |
| self.expr_span.shrink_to_lo(), | ||
| "dereference the expression", | ||
| "*", | ||
| Applicability::MachineApplicable, |
There was a problem hiding this comment.
prefer to use MaybeIncorrect?
since if we apply the suggestion there maybe still some errors:
error[E0507]: cannot move out of `*priority` which is behind a shared reference
--> tests/ui/cast/cast-enum-to-int-issue-151116.rs:10:20
|
10 | let priority = *priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
| ^^^^^^^^^ move occurs because `*priority` has type `Priority`, which does not implement the `Copy` trait
|
note: if `Priority` implemented `Clone`, you could clone the value
--> tests/ui/cast/cast-enum-to-int-issue-151116.rs:2:1
|
2 | enum Priority {
| ^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
10 | let priority = *priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid
| --------- you could clone this value|
|
||
| fn main() { | ||
| let priority = &Priority::Normal; | ||
| let priority = priority as u8; //~ ERROR casting `&Priority` as `u8` is invalid |
There was a problem hiding this comment.
could add a test to make sure it does not suggest for code:
let priority = &Priority::Normal as u8|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
I will fix this up next week, thanks! Didn't see this. |
Fixes #151116
A more descriptive error message when casting an enum to an Integer. Please review issue linked above.