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

size_of_in_element_count: Disable lint on division by byte-size #6578

Merged

Conversation

MarijnS95
Copy link
Contributor

Fixes #6511

It is fairly common to divide some length in bytes by the byte-size of a single element before creating a from_raw_parts slice or similar operation. This lint would erroneously disallow such expressions.

Just in case, instead of simply disabling this lint in the RHS of a division, keep track of the inversion and enable it again on recursive division.


changelog: Do not trigger size_of_in_element_count when dividing by element size

@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 @flip1995 (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 Jan 12, 2021
@MarijnS95
Copy link
Contributor Author

@nico-abram Is there by chance a test you'd be fine to remove? I guess it should be fine to test all affected functions just once to make sure they trigger, then test all the expression variants on just one function (ie. no need to test size_of and size_of_val, or a certain multiplication pattern multiple times).

@nico-abram
Copy link
Contributor

I think most of the tests left are testing different functions/methods. Maybe you can combine some of the element count expression tests with tests for specific functions?

Like removing unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of_val(&x[0])) }; and putting that expression in one of the other calls

If possible IMO keeping each case as cleanly separated as possible is best but I'm not a maintainer. Whoever reviews this can probably give you better feedback

@MarijnS95
Copy link
Contributor Author

@nico-abram Correct, in hindsight the top half tests various functions (ie. none has been accidentally skipped from the linting code), the bottom half tests all expressions. There was only one overlapping thing with copy() which has been removed to get back below 200 lines.

I guess the only recommended solution beyond that is to split the test out in two parts (one for function inclusion, the other for variation in the expressions), but that doesn't seem necessary in this MR at least (unless one wishes to re-add the test for copy()).

@ThibsG
Copy link
Contributor

ThibsG commented Jan 16, 2021

On another side, do you think it's relevant to also fix #6590 here?

@MarijnS95
Copy link
Contributor Author

MarijnS95 commented Jan 16, 2021

@ThibsG That looks like exactly the mistake this lint is protecting against. The function described there takes a single u8 as input, that interpreted as slice should have a constant length of 1. I guess it only makes more sense to use the constant 1 or the output from .len(), not size_of::<u8> nor multiplied by that.

Otherwise, matching types left and right is a completely different issue (and iirc why this lint doesn't trigger when the type on the left and right is different).

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.

Changes LGTM

There was only one overlapping thing with copy() which has been removed to get back below 200 lines.

This restriction in Clippy is there to keep tests to a maintainable size. Please don't remove tests because of this. Your suggestion of splitting up the tests sounds reasonable to me. Please do this in a separate commit. You can create a directory for tests for this lint or just append the test file with _{function,expression} or similar.

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 17, 2021
@MarijnS95 MarijnS95 force-pushed the size-in-element-count-divide-by-byte-size branch 2 times, most recently from 2c6c0a1 to 33c8bc2 Compare January 19, 2021 18:41
@MarijnS95
Copy link
Contributor Author

MarijnS95 commented Jan 19, 2021

@flip1995 Thanks for confirming, the tests are now split up in two files distinguishing function "completeness" and expression variants, obviously in separate commits 😉

If I understand the docs correctly, I should now (be able to) do this:

@rustbot label -S-waiting-on-author +S-waiting-on-review

Right?

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 19, 2021
…ions

An upcoming test case for new expresssion variants make the stderr file
go over 200 lines. Split this test case in two to have a clear
distinction between checking whether the lint is still applying on
all the functions with member counts as argument, versus validating
various member-count expressions that may or may not be invalid.
It is fairly common to divide some length in bytes by the byte-size of a
single element before creating a `from_raw_parts` slice or similar
operation. This lint would erroneously disallow such expressions.

Just in case, instead of simply disabling this lint in the RHS of a
division, keep track of the inversion and enable it again on recursive
division.
@MarijnS95 MarijnS95 force-pushed the size-in-element-count-divide-by-byte-size branch from 33c8bc2 to d4bf59b Compare January 19, 2021 19:06
@flip1995
Copy link
Member

@bors r+

Thanks! Great job!

@bors
Copy link
Collaborator

bors commented Jan 20, 2021

📌 Commit d4bf59b has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Jan 20, 2021

⌛ Testing commit d4bf59b with merge fb0d7f1...

@bors
Copy link
Collaborator

bors commented Jan 20, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing fb0d7f1 to master...

@bors bors merged commit fb0d7f1 into rust-lang:master Jan 20, 2021
@MarijnS95 MarijnS95 deleted the size-in-element-count-divide-by-byte-size branch January 20, 2021 08:40
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.

size_of_in_element_count false-positive when dividing byte-size by element size
7 participants