rust-lang / rust-lang/rust-clippy

False positive in trivially_copy_pass_by_ref where reference is used

Open
#5,645 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug E-hard I-false-positive I-suggestion-causes-error S-needs-discussion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

I've encountered a false positive with the trivially_copy_pass_by_ref lint. While Clippy is able to tell that a function takes a &u8 instead of a u8, it doesn't seem to be checking whether that reference is passed to another function that requires a reference rather than a value. My example:

// Clippy says:
// error: this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
pub fn contains(&self, target: &u8) -> bool {
    exists_within(some_bytes, target)
}

pub fn exists_within<'a, T>(slice: &'a [T], reference: &'a T) -> bool {
    unimplemented!()
}

Its recommended fix (to change it to a u8) won't work here, because the function the reference is passed to requires it to be a reference.

I made a quick Rust playground with the exact code that ran into this issue.

Thanks for Clippy!


$ cargo clippy -V
clippy 0.0.212 (204bb9b5 2020-03-17)

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

Start with the trivially_copy_pass_by_ref lint and reproduce the report using the Rust example or linked playground, running cargo clippy. Done means the lint no longer recommends changing a reference that must be passed to another function by reference, while its intended warnings remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.