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
Lint telling me to needlessly increase memory footprint #12786
Comments
taking a non |
I'm unsure if not linting would be desirable: If the value is smaller than a reference (which is platform-dependent to begin with), having anything to gain is very strongly dependent on what the compiler did at the specific call-site, most importantly considering inlining. Passing a one-byte type by value instead of by reference does not present the compiler with an opportunity to save seven bytes, e.g. if the value is passed by register (which consumes the full register anyway). |
if developing the user's muscle memory is the goal, then why can't we include the step of "check argument's size before deciding whether to pass reference"? You have a good point when you say the reference size is platform dependent. How about taking the range of all possible reference sizes on modern platforms? Then the linter could say "remove reference" if the argument size is below the range of possible reference sizes. And linter would say "add reference" if the argument size is above the range of possible reference sizes. |
|
i think there are only a handful reasons to make a type that is smaller than if the type is non-Copy and equal to or larger in size than |
Summary
clippy::needless_pass_by_value
needlessly tells the user to increase the memory footprint by choosing reference (8 bytes) rather than cloned/owned value when the latter is less than 8 bytes. When the latter is less than 8 bytes (might be different than 8 bytes depending on the hardware's register size, etc), the lint should instead advise the reference to be removed. And if both options have the same memory footprint, then lint should be silent.Lint Name
needless_pass_by_value
Reproducer
I tried this code:
I saw this warning happen:
I expected to see this happen:
"Lol" takes only 1 byte, but reference takes 8 bytes. This lint is telling me to choose 8 bytes (reference) over 1 byte (clone) which is harmful advice. Therefore I expect the following:
lol: Lol
)Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: