rust-lang / rust-lang/rust-clippy

Lint telling me to needlessly increase memory footprint

Open
#12,786 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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:

#![warn(clippy::needless_pass_by_value)]

fn main() {}

pub fn lol_func(lol: Lol) -> Lol {
    lol.clone()
}

#[derive(Clone)]
pub enum Lol {
    Variant1,
    Variant2,
}

I saw this warning happen:

consider taking a reference instead: `&Lol`clippy[needless_pass_by_value](https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value)
main.rs(5, 22): original diagnostic
this argument is passed by value, but not consumed in the function body
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_valueclippy[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B0%5D?0#file%3A%2F%2F%2Fhome%2Famir-abdin%2Fsrc%2Fnice%2Fnice%2Fsrc%2Fmain.rs)
main.rs(10, 1): consider marking this type as `Copy`
main.rs(1, 9): the lint level is defined here
main.rs(5, 22): consider taking a reference instead: `&Lol`

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:

  1. Clippy checks the memory footprint of reference
  2. Clippy checks the memory footprint of the argument type (in this case lol: Lol)
  3. Compare
  4. if reference has lower memory footprint, then activate lint. If clone has lower memory footprint, then warn about the need to remove reference and use clone. If both have the same memory footprint, then silence the lint.
Version
rustc 1.77.0-nightly (d5fd09972 2024-01-22)
binary: rustc
commit-hash: d5fd0997291ca0135401a39dff25c8a9c13b8961
commit-date: 2024-01-22
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the implementation and existing tests for the needless_pass_by_value lint, then run the supplied Rust reproducer on the current toolchain. Compare the argument and reference footprints across the reported cases; done means the lint advises only when appropriate, stays silent for equal sizes, and tests cover the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.