-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 lint size_of_in_element_count #6394
Conversation
r? @ebroto (rust-highfive has picked a reviewer for you, use r? to override) |
@nico-abram Do you have a nightly toolchain with rustfmt installed? You can check this by running |
Thanks @flip1995 , you're completely right ( |
@nico-abram You have to use the nightly version. So it should work. |
Mhm. Here's the output from both commands:
I'm not getting errors from |
430a292
to
a06f548
Compare
ea8b52b
to
336e41d
Compare
@thomcc it should handle those now |
:D Wonderful! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
Besides the nits, do you think it would make sense to also lint in these cases?
- Functions from
std::ptr
- slice_from_raw_parts
- slice_from_raw_parts_mut
- swap_nonoverlapping
- write_bytes
- Methods from pointer primitive
- write_bytes
@ebroto Sounds good to me! Will try to fix the change requests and add those today Looking for "count:" in the ptr primitive docs I also came across these: wrapping_sub, wrapping_add, add, sub, offset, wrapping_offset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing the changes so quickly!
Looking for "count:" in the ptr primitive docs I also came across these: wrapping_sub, wrapping_add, add, sub, offset, wrapping_offset
Yeah I saw them and initially also thought that they were not related to this lint, but after re-reading the documentation it seems to me the same problem may appear if someone uses size_of/size_of_val
, right?
I think it may make sense to lint also on these, and maybe change the name of the lint to not make reference to copy
or unsafe
, because slice_from_raw_parts/slice_from_raw_parts_mut
are not unsafe
. We should also avoid referring to these terms in the error message.
AIUI, yes, because they also work in units of T
Seems good to me. I think I was thinking of the slice counterparts of those two when I assumed they were unsafe. Do you think the lint should also happen on those 2? (Their docs say Does |
Good catch! I think the same logic applies.
I think it respects the naming convention, but I would be more inclined for |
|
efc90e3
to
faad9a1
Compare
I'm not sure why CI is failing. It seems to be an ICE test. Could rustc's error output have changed? (Assuming CI is using nightly) |
Error: The feature Please let |
I think the current CI failure is (as mentioned here) indeed upstream rustc-repo-clippy changes that are waiting to be synced |
☔ The latest upstream changes (presumably #6415) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just needs a rebase
UNSAFE_SIZEOF_COUNT_COPIES, | ||
expr.span, | ||
SIZE_OF_IN_ELEMENT_COUNT, | ||
count_expr.span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
Specifically: - find std::ptr::write_bytes - find std::ptr::swap_nonoverlapping - find std::ptr::slice_from_raw_parts - find std::ptr::slice_from_raw_parts_mut - pointer_primitive::write_bytes
Also fix review comments: - Use const arrays and iterate them for the method/function names - merge 2 if_chain's into one using a rest pattern - remove unnecessary unsafe block in test And make the lint only point to the count expression instead of the entire function call
faad9a1
to
8322e30
Compare
Specifically ptr::{sub, wrapping_sub, add, wrapping_add, offset, wrapping_offset} and slice::{from_raw_parts, from_raw_parts_mut} The lint now also looks for size_of calls through casts (Since offset takes an isize)
8322e30
to
c1a5329
Compare
@bors r+ |
📌 Commit c1a5329 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #6381
changelog: Add lint to check for using size_of:: or size_of_val:: in the count parameter to ptr::copy or ptr::copy_nonoverlapping, which take a count of Ts (And not a count of bytes)
.stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt
Running
cargo test
locally fails with this error:But I did run
cargo dev fmt