Should this code give "comparison is useless due to type limits" error?

I think the code below could return "comparison is useless due to type limits".

Here, I test if a u8 is greater than u8::max_value(), which is not possible.

I guess the compiler is checking whether the right-hand value can be fitted within an u8.

#![allow(unused)]
fn main() {
    println!("Largest u8: {}", u8::max_value());
    let i = u8::max_value();
    // The code below could be catched as an attempt to make a
    // comparison that is "useless du to type limits",
    // just as it would `if i > 1000` would.
    if i > u8::max_value() {
        println!("yes");
    }
}

(Playground)

1 Like

The compiler doesn't seem to recognize this error on anything other than literals.
Example with std::u8::MAX:

#![allow(unused)]
fn main() {
    println!("Largest u8: {}", std::u8::MAX);
    let i = std::u8::MAX;
    if i > std::u8::MAX {
        println!("yes");
    }
}

No error. Clippy however catches it, so maybe max_value should be caught too.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.