-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Is there any reason why the "integer out of range" lint doesn't trigger when you cast a C-style enum to an integer type which isn't big enough?
For example say I have the following program:
enum Foo {
A = 1,
C = 1234,
}
fn main() {
let a = Foo::A as u32;
let c = Foo::C as u8;
let normal = 1234 as u8;
println!("{}, {}, {}", a, c, normal);
}
When you compile it, you get a warning on the integer cast, but not the Foo::C as u8
line.
Compiling playground v0.0.1 (file:///playground)
warning: literal out of range for u8
--> src/main.rs:9:18
|
9 | let normal = 1234 as u8;
| ^^^^
|
= note: #[warn(overflowing_literals)] on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.46 secs
Running `target/debug/playground`
1, 210, 210
I would have thought the compiler knows what integer size a C-style enum can fit into and then be able to lint accordingly. So for this case a Foo
's largest variant has a value of 1234
meaning it can fit into a u16
(or i16
), but not a u8
.
vlisivka and petertodd
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.