Skip to content

lint for integer overflow, especially in release mode #12503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bingmatv opened this issue Mar 18, 2024 · 4 comments
Open

lint for integer overflow, especially in release mode #12503

bingmatv opened this issue Mar 18, 2024 · 4 comments
Labels
A-lint Area: New lints

Comments

@bingmatv
Copy link

bingmatv commented Mar 18, 2024

What it does

Rust panics for integer overflow in debug mode, but it will wrap around any integers in release mode, aka, when --release parameter is added for cargo.

Advantage

No response

Drawbacks

No response

Example

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s = (s.trim().parse::<u8>().unwrap()) * 3;
}

It accepts user input then multiply the accepted integer by 3, may wrap around when multiply by 3.

@bingmatv bingmatv added the A-lint Area: New lints label Mar 18, 2024
@taiki-e
Copy link
Member

taiki-e commented Mar 19, 2024

This seems to be covered by arithmetic_side_effects?

@bingmatv
Copy link
Author

This seems to be covered by arithmetic_side_effects?

cargo clippy only showed:
warning: unused variable: s
--> src/main.rs:4:9
|
4 | let s = (s.trim().parse::().unwrap()) * 3;
| ^ help: if this is intentional, prefix it with an underscore: _s
Only warned about unused variable.

@taiki-e
Copy link
Member

taiki-e commented Mar 19, 2024

arithmetic_side_effects is in the restriction lint group, which is allowed by default, so you have to explicitly enable it.

#![warn(clippy::arithmetic_side_effects)] // <-------------
#![allow(dead_code, unused_variables)]

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s = (s.trim().parse::<u8>().unwrap()) * 3;
}
warning: arithmetic operation that can potentially result in unexpected side-effects
 --> src/main.rs:7:13
  |
7 |     let s = (s.trim().parse::<u8>().unwrap()) * 3;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::arithmetic_side_effects)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

playground

@ijackson
Copy link

Unfortunately arithmetic_side_effects is quite poor:

But probably it doesn't make sense to use this ticket as a tracking issue for problems with this lint; it shoud be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants