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 suggest .saturating_add/sub(x) for .checked_add/sub(x).unwrap_or(MAX/MIN) #1557

Closed
sinkuu opened this issue Feb 19, 2017 · 0 comments · Fixed by #4498
Closed

Lint to suggest .saturating_add/sub(x) for .checked_add/sub(x).unwrap_or(MAX/MIN) #1557

sinkuu opened this issue Feb 19, 2017 · 0 comments · Fixed by #4498
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types

Comments

@sinkuu
Copy link
Contributor

sinkuu commented Feb 19, 2017

unsigned.checked_add(x).unwrap_or(unsigned::MAX)
 -> unsigned.saturating_add(x)
unsigned.checked_sub(x).unwrap_or(0)
 -> unsigned.saturating_sub(x)

Signed versions can also be linted if literals are used.

signed.checked_add(non_negative_literal).unwrap_or(signed::MAX)
 -> signed.saturating_add(x)
signed.checked_add(    negative_literal).unwrap_or(signed::MIN)
 -> signed.saturating_add(x)

signed.checked_sub(non_negative_literal).unwrap_or(signed::MIN)
 -> signed.saturating_sub(x)
signed.checked_sub(    negative_literal).unwrap_or(signed::MAX)
 -> signed.saturating_sub(x)
@mcarton mcarton added good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints T-middle Type: Probably requires verifiying types labels Feb 19, 2017
@bors bors closed this as completed in ffe57fa Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants