Arithmetic overflow panic despite allow(arithmetic_overflow) attribute

Whether I use the inner or outer attribute, the following snippet panics with "attempt to add with overflow"

fn main() {
    // #![allow(arithmetic_overflow)]
    let a: u32 = (1 << 31) + 322;
    let _ = a + (1 << 31);

I don't want to use Wrapping since I'm doing lots of arithmetic, it would be a real hassle.


#[allow(arithmetic_overflow)] disables the arithmetic_overflow compile-time lint. It doesn't change the behavior of the arithmetic operations.

1 Like

@sfackler I don't understand. When I compile for release, overflow doesn't crash the app. When I compile for debug, overflow crashes the app. Can't I turn off that debug warning? Maybe some argument to pass to the compiler, like force-overflow-checks=no?

You can disable overflow checks with a flag in Cargo.toml:

overflow-checks = false 
1 Like

Oh, I see, I was trying that in the package Cargo.toml where it has no effect. I had to move it to the workspace Cargo.toml.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.