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

Add manual_saturating_arithmetic lint #4498

Merged
merged 3 commits into from
Sep 4, 2019

Conversation

sinkuu
Copy link
Contributor

@sinkuu sinkuu commented Sep 4, 2019

changelog: add manual_saturating_arithmetic lint

Fixes #1557. This lint detects manual saturating arithmetics like x.checked_add(10u32).unwrap_or(u32::max_value()) and suggests replacing with x.saturating_add(10u32).

@sinkuu sinkuu changed the title Add checked_arith_unwrap_or lint Add manual_saturating_arithmetic lint Sep 4, 2019
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Completely new lint and I don't even have a single thing to complain about. 🎉

Thanks!

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, found something 😄

clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/mod.rs Show resolved Hide resolved
@flip1995
Copy link
Member

flip1995 commented Sep 4, 2019

Thanks!

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 4, 2019

📌 Commit c6fb9c8 has been approved by flip1995

bors added a commit that referenced this pull request Sep 4, 2019
Add manual_saturating_arithmetic lint

changelog: add `manual_saturating_arithmetic` lint

Fixes #1557. This lint detects manual saturating arithmetics like `x.checked_add(10u32).unwrap_or(u32::max_value())` and suggests replacing with `x.saturating_add(10u32)`.
@bors
Copy link
Collaborator

bors commented Sep 4, 2019

⌛ Testing commit c6fb9c8 with merge ffe57fa...

@tesuji
Copy link
Contributor

tesuji commented Sep 4, 2019

I suggest using syntax::ast::LitIntType in syntax::ast::LitKind::Int. This would make the code clearer.

@sinkuu
Copy link
Contributor Author

sinkuu commented Sep 4, 2019

@Izutao There's LitIntType::Unsuffixed variant, so I'd need to rely on type information anyway.

@bors
Copy link
Collaborator

bors commented Sep 4, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing ffe57fa to master...

@bors bors merged commit c6fb9c8 into rust-lang:master Sep 4, 2019
@bors bors mentioned this pull request Sep 4, 2019
@sinkuu sinkuu deleted the checked_arithmetic_unwrap branch September 4, 2019 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint to suggest .saturating_add/sub(x) for .checked_add/sub(x).unwrap_or(MAX/MIN)
4 participants