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 large_types_passed_by_value lint #6135

Merged
merged 1 commit into from
Oct 24, 2020
Merged

Add large_types_passed_by_value lint #6135

merged 1 commit into from
Oct 24, 2020

Conversation

cauebs
Copy link
Contributor

@cauebs cauebs commented Oct 8, 2020

Creates a new lint that checks for large function parameters passed by value. Types that are Copy were ignored, because I understand they are explicitly marked as being cheap (or just acceptable) to copy. Arrays were excluded from that restriction because they are always Copy, independently of the size, if the elements are Copy. Only Copy types are considered, because if a type is not Copy it cannot be dereferenced, as pointed out by @ebroto, and that would require analyzing the whole function body to check if the argument is moved somewhere else. self and mut parameters are also skipped.

I refactored trivially_copy_pass_by_ref and the new lint into a new pass_by_ref_or_value module, since both share most of their implementations, as was pointed out to me in #4499.

Some questions that remain:
1. Should self be disregarded for this lint?
2. Should we special case types like Vec and String when suggesting changes? (to slices and &str)
3. What should be the default size limit?
4. Are there any special cases I'm missing?

I ask the maintainers to add the hacktoberfest-accepted label if this PR is decent, even if there are some minor details to work out, but I do intend to stick around and finish the job.

Fixes #4499

changelog: Add new lint [large_types_passed_by_value]

@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 @matthiaskrgr (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 8, 2020
@ebroto ebroto assigned ebroto and unassigned matthiaskrgr Oct 8, 2020
Copy link
Member

@ebroto ebroto 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 your contribution to Clippy! I've left some remarks.

About your open questions:

  1. I think we should exclude self as you suggest because it's a well-known pattern to consume a value, regardless of the efficiency.
  2. Vec and String should never trigger the lint as they are quite small in the stack (and are not Copy, see my other comment).
  3. and
  4. I hope I've answered those in the review itself.

Also, the empty stdout file should be removed.


cc @flip1995 if you have the time/energy, I would not mind another pair of eyes on this, as some of my suggestions are quite opinionated :)

clippy_lints/src/lib.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/utils/conf.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
tests/ui/large_type_pass_by_value.rs Outdated Show resolved Hide resolved
@ebroto
Copy link
Member

ebroto commented Oct 9, 2020

Also, FYI we should fix the dogfood test. Hopefully after the suggestions are applied there should be less (if anything) to modify.

@ebroto ebroto 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 Oct 9, 2020
@flip1995
Copy link
Member

I ask the maintainers to add the hacktoberfest-accepted label if this PR is decent, even if there are some minor details to work out, but I do intend to stick around and finish the job.

That shouldn't be necessary. PRs are automatically accepted in this repo:

PRs count if:
Submitted during the month of October AND 
Submitted in a public repo AND (
  The PR is labelled as hacktoberfest-accepted by a maintainer OR
  Submitted in a repo with the hacktoberfest topic AND (            <-- This applies for Clippy
    The PR is merged OR
    The PR has been approved
  )
)

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.

There's an empty *.stdout file that needs to be removed.


About Vec and String: You shouldn't get sizes bigger than 16/24Bytes on 64 bit machines, so they can always be safely passed by value. If someone sets the limit as low as that, there is another lint for &Vec and &String suggesting a slice or &str. So I don't think we need to deal with that here.

clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/utils/conf.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Oct 16, 2020

☔ The latest upstream changes (presumably #6178) 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:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@cauebs cauebs force-pushed the master branch 2 times, most recently from 082c804 to 78d14a9 Compare October 19, 2020 07:50
@cauebs
Copy link
Contributor Author

cauebs commented Oct 19, 2020

Ok, I think I addressed all your suggestions! Thanks a lot for the thorough feedback, @ebroto and @flip1995, I learned a lot more than I expected with this PR.

Let me know if there's anything else to improve here, but I might only work on it again next weekend.

@cauebs cauebs requested a review from ebroto October 19, 2020 08:01
@cauebs cauebs changed the title Add large_type_pass_by_value lint Add large_types_passed_by_value lint Oct 19, 2020
@ebroto ebroto 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 Oct 21, 2020
Copy link
Member

@ebroto ebroto left a comment

Choose a reason for hiding this comment

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

Only a small change and some tests to add, and this should be ready to merge!

Also, could you rebase to get rid of the merge commits and squash to avoid intermediate commits?

clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/pass_by_ref_or_value.rs Outdated Show resolved Hide resolved
tests/ui/large_types_passed_by_value.rs Show resolved Hide resolved
tests/ui/large_types_passed_by_value.rs Show resolved Hide resolved
tests/ui/large_types_passed_by_value.rs Show resolved Hide resolved
@ebroto ebroto 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 Oct 24, 2020
Refactor trivially_copy_pass_by_ref and the new lint into pass_by_ref_or_value module

Update stderr of conf_unknown_key test

Rename lint to large_types_passed_by_value

Increase `pass_by_value_size_limit` default value to 256

Improve rules for `large_types_passed_by_value`

Improve tests for `large_types_passed_by_value`

Improve documentation for `large_types_passed_by_value`

Make minor corrections to pass_by_ref_or_value.rs suggested by clippy itself

Fix `large_types_passed_by_value` example and improve docs

pass_by_ref_or_value: Tweak check for mut annotation in params

large_types_passed_by_value: add tests for pub trait, trait impl and inline attributes
@ebroto
Copy link
Member

ebroto commented Oct 24, 2020

@bors r+

Thanks for your work on this lint!

@bors
Copy link
Contributor

bors commented Oct 24, 2020

📌 Commit e8731a9 has been approved by ebroto

@bors
Copy link
Contributor

bors commented Oct 24, 2020

⌛ Testing commit e8731a9 with merge 5c78d26...

@bors
Copy link
Contributor

bors commented Oct 24, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: ebroto
Pushing 5c78d26 to master...

@bors bors merged commit 5c78d26 into rust-lang:master Oct 24, 2020
@cauebs
Copy link
Contributor Author

cauebs commented Oct 25, 2020

And thank you guys for the feedback and help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: Pass references when possible for large data types?
6 participants