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

extension to manual_saturating_arithmetic #10070

Open
tomtau opened this issue Dec 13, 2022 · 1 comment · May be fixed by #12476
Open

extension to manual_saturating_arithmetic #10070

tomtau opened this issue Dec 13, 2022 · 1 comment · May be fixed by #12476
Labels
A-lint Area: New lints

Comments

@tomtau
Copy link

tomtau commented Dec 13, 2022

What it does

Right now, the lint checks for patterns with checked_add and unwrap_or. It could perhaps be extended (or it could be a new lint which covers manual wrapping or checked arithmetic as well) to also check for other patterns that don't use checked_add. For example with unsigned integers:

let result = if a > b { a - b } else { 0 };

which could be rewritten as:

let result = a.saturating_sub(b);

Lint Name

manual_saturating_arithmetic

Category

complexity

Advantage

  • simpler code
  • more coverage over the original lint

Drawbacks

  • more complex checking

Example

let result = if a > b { a - b } else { 0 };

Could be written as:

let result = a.saturating_sub(b);
@tomtau tomtau added the A-lint Area: New lints label Dec 13, 2022
@djc
Copy link
Contributor

djc commented Feb 1, 2024

I just ran into some examples of this as well. Would be great to add!

djc/bb8@67b7e60

@GuillaumeGomez GuillaumeGomez linked a pull request Mar 13, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants