Skip to content

False positive in trivially_copy_pass_by_ref where reference is used #5645

Open
@ogham

Description

@ogham

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedS-needs-discussionStatus: Needs further discussion before merging or work can be started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions