Unexpected panic

As we all know, panic in Rust is a quite serious problem, we will do everything we can to avoid it. i.e., no unwrap ... etc. force developer to deal with all situations. But I ran into a panic which really surprises me. Basically:

let x: uint = 8; let y: uint = 9;

I meant to compare: if x < y { } ,which works fine. but the other day, for some reason, I wrote: if x - y < 0 {} And the system simply panics. I know why it panics, but I really feel rust compiler should somehow prevent me from making this type of mistakes, at least give me a warning or something?

This specific condition warns for unsigned integers:

warning: comparison is useless due to type limits
 --> src/main.rs:4:8
  |
4 |     if x - y < 0 {}
  |        ^^^^^^^^^
  |
  = note: `#[warn(unused_comparisons)]` on by default
4 Likes

I didn't notice that 2 years ago. :slight_smile: Maybe this lint is new. This is cool!

There are plenty of unexpected things that can cause panics. i8::MIN / -1 will panic, for example (though if you just write that, the unconditional_panic lint will catch it)