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

Update the existing arithmetic lint #6229

Merged
merged 2 commits into from
Oct 30, 2020
Merged

Update the existing arithmetic lint #6229

merged 2 commits into from
Oct 30, 2020

Conversation

h3nill
Copy link
Contributor

@h3nill h3nill commented Oct 26, 2020

re: #6209

Updates the lint to not the error message if RHS of binary operation / of % is a literal/constant that is not 0 or -1, as suggested here

changelog: Expand [integer_arithmetic] to work with RHS literals and constants

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @phansch (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 26, 2020
Copy link
Contributor

@mlegner mlegner left a comment

Choose a reason for hiding this comment

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

Disclaimer: I'm not a maintainer of the clippy repo, I just came across your PR as I was also looking at the related issue.

I see a few problems with this PR:

  • As far as I understand the float-arithmetic lint, this is supposed to check for all floating-point arithmetic. In contrast, the integer-arithmetic lint only considers those that can overflow or panic. That means, that you should probably still trigger the float-arithmetic lint on all divisions, even when the denominator is a non-zero literal.
  • You are now only triggering the integer-arithmetic lint for the literals 0 and -1 but not for other denominators that might still cause overflow or panic.
  • Tests for the integer-arithmetic lint with a denominator of 0 or -1 (for division and remainder) would be nice.

clippy_lints/src/arithmetic.rs Outdated Show resolved Hide resolved
clippy_lints/src/arithmetic.rs Outdated Show resolved Hide resolved
clippy_lints/src/arithmetic.rs Outdated Show resolved Hide resolved
@h3nill
Copy link
Contributor Author

h3nill commented Oct 27, 2020

First of all, Thank you for the review.

As far as I understand the float-arithmetic lint, this is supposed to check for all floating-point arithmetic. In contrast, the integer-arithmetic lint only considers those that can overflow or panic. That means, that you should probably still trigger the float-arithmetic lint on all divisions, even when the denominator is a non-zero literal.

Oh right.

You are now only triggering the integer-arithmetic lint for the literals 0 and -1 but not for other denominators that might still cause overflow or panic.

I made those changes referring to this comment, I was also not able to think of any other literals that might cause overflow or panic. Do you know of any?
EDIT: Oh I see, currently it does not trigger if the value is not literal.

Tests for the integer-arithmetic lint with a denominator of 0 or -1 (for division and remainder) would be nice.

Sure.

@h3nill h3nill changed the title Update the existing arithmetic lint [WIP]Update the existing arithmetic lint Oct 27, 2020
@h3nill
Copy link
Contributor Author

h3nill commented Oct 28, 2020

Made the requested changes.

@h3nill h3nill changed the title [WIP]Update the existing arithmetic lint Update the existing arithmetic lint Oct 28, 2020
Copy link
Contributor

@mlegner mlegner left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! 👍

Looking at the errors produced by the tests, I noticed that a division by literal 0 (and remainder with a divisor of 0) is already caught by rustc, see for example here.

I'm not sure what the policy is regarding redundancy between rustc and rust-clippy, but it seems to me that it's not too helpful to produce errors multiple times for the same code. Probably @phansch knows what to do here.

clippy_lints/src/arithmetic.rs Outdated Show resolved Hide resolved
tests/ui/integer_arithmetic.rs Outdated Show resolved Hide resolved
@h3nill
Copy link
Contributor Author

h3nill commented Oct 29, 2020

(Refactored to reduce duplication.)

@phansch
Copy link
Member

phansch commented Oct 30, 2020

Thanks for the PR and the reviews alike both of you 💙

not too helpful to produce errors multiple times for the same code

Definitely not - I think it would be enough for us to skip linting on division/modulo by 0 here and let rustc handle that part? Apart from that, the rest looks good to me 👍

(Ideally there would be a generic way for Rust/Clippy to only emit one lint per span, but that's not something for this PR and needs some groundwork in rustc, see Zulip here)

@h3nill
Copy link
Contributor Author

h3nill commented Oct 30, 2020

Definitely not - I think it would be enough for us to skip linting on division/modulo by 0 here and let rustc handle that part? Apart from that, the rest looks good to me +1

Yeah, I also think having the same error twice is not ideal. I will remove that part.

@phansch
Copy link
Member

phansch commented Oct 30, 2020

@bors r+ thanks!

@bors
Copy link
Contributor

bors commented Oct 30, 2020

📌 Commit fa0a78b has been approved by phansch

@bors
Copy link
Contributor

bors commented Oct 30, 2020

⌛ Testing commit fa0a78b with merge 0be6544...

@bors
Copy link
Contributor

bors commented Oct 30, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: phansch
Pushing 0be6544 to master...

@bors bors merged commit 0be6544 into rust-lang:master Oct 30, 2020
@h3nill h3nill deleted the improve-integer-division-lint branch October 30, 2020 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants