Skip to content
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

Lint to disallow unsigned subtraction #45

Open
ppershing opened this issue Sep 19, 2023 · 1 comment
Open

Lint to disallow unsigned subtraction #45

ppershing opened this issue Sep 19, 2023 · 1 comment
Labels
A-user-story Area: A user story or a related issue

Comments

@ppershing
Copy link

Lint explanation

Unsigned subtraction is likely to introduce underflow bugs. These bugs won't be caught in release mode.

Example code

Lint should disallow following code

let len: usize = vec.len();
let max_len: usize = 1000;
let available_space = max_len - len; // likely to underflow

while allowing both

let space = max_len.wrapping/saturating/checked_sub(len)

and

let len: isize = vec.len() as i32;
let max_len: isize = 1000;
let space = max_len - len;
@ppershing ppershing added the A-user-story Area: A user story or a related issue label Sep 19, 2023
@xFrednet
Copy link
Member

Thank you for the issue. This lint should already be possible, I want to create some example lints and this one might be a good and simple example. I'll implement it after the next release at the start of October.

Btw, you might want to take a look at clippy::arithmetic_side_effects which checks for all arithmetic operators that can cause a panic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-user-story Area: A user story or a related issue
Projects
None yet
Development

No branches or pull requests

2 participants