Skip to content

Missing some warnings about "literal out of range for its type" #14165

Closed
@ghost

Description

The places I'd expect to get warnings are indicated by the comments in this example:

fn main() {
    let a: u8 = 1000;
    let b = 1000u8;
    let c = 1000 as u8; // NO WARNING !!!

    let one: u8 = 1;
    let d = (one * 1000) as u32;
    let e = (one *   -1) as u32; // NO WARNING !!!
    let f = (one * -255) as u32; // NO WARNING !!!
    let g = (one * -256) as u32;

    println!("{}, {}, {}, {}, {}, {}, {}", a, b, c, d, e, f, g);
}

Here's the compiler output:

warning: literal out of range for its type, #[warn(type_overflow)] on by default
let a: u8 = 1000;
            ^~~~
warning: literal out of range for its type, #[warn(type_overflow)] on by default
let b = 1000u8;
        ^~~~~~
warning: literal out of range for its type, #[warn(type_overflow)] on by default
let d = (one * 1000) as u32;
               ^~~~
warning: literal out of range for its type, #[warn(type_overflow)] on by default
let g = (one * -256) as u32;
                ^~~

And here's the program output:
232, 232, 232, 232, 255, 1, 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions