Hi friends!
I am wondering if this should be considered a bug:
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
enum Digit {
Zero = 0,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
}
impl Digit {
fn make_digits_from(n: u64) -> Vec<Self> {
n.to_string()
.bytes()
.map(|b| unsafe { std::mem::transmute(b - b'0') })
.collect()
}
}
This generates this warning:
warning: multiple variants are never constructed
--> say/src/lib.rs:45:5
|
43 | enum Digit {
| ----- variants in this enum
44 | Zero = 0,
45 | One,
| ^^^
46 | Two,
| ^^^
47 | Three,
| ^^^^^
48 | Four,
| ^^^^
49 | Five,
| ^^^^
50 | Six,
| ^^^
51 | Seven,
| ^^^^^
52 | Eight,
| ^^^^^
53 | Nine,
| ^^^^
|
Yes, I know Digit
is not public, it is being used/constructed in another function in order to create an English representation of the number (1234
-> "one thousand two hundred thirty-four"
). However, it is clearly using and constructing each digit (it does successfully generate the English representation from Vec<Digit>
). I think the issue lies with my use of std::mem::transmute
, perhaps because the compiler doesn't check for the use of transmute
resulting in all possible variants.
I see a potential issue with fixing this, which is that if one uses transmute
, one has to manually check if all variants are actually covered. But this is already the case as it is, because you have to add #[allow(dead_code)]
to get rid of the warnings anyway. So is there really a benefit to the compiler saying variants are "not constructed" in this case?
I appreciate any clarification the community might have on why this is the case, and if it is likely to be fixed.
I'm using rust 1.80.0
on NixOS x86_64 with rustup
.