Skip to content

Is needless_pass_by_value behaving properly? #3031

Closed
@gnzlbg

Description

@gnzlbg

This (playground):

pub enum Algorithm {
    Scalar,
    Simd,
}
pub trait O {}
fn foo<O>(_o: O) {}
fn bar<O>(_o: O) {}

pub fn run<T: O>(o: T, alg: Algorithm) {
    match alg {
        Algorithm::Scalar => foo(o),
        Algorithm::Simd => bar(o),
    }
}

fn main() {}

errors with

warning: this argument is passed by value, but not consumed in the function body
 --> src/main.rs:6:15
  |
6 | fn foo<O>(_o: O) {}
  |               ^ help: consider taking a reference instead: `&O`
  |
  = note: #[warn(needless_pass_by_value)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#needless_pass_by_value

warning: this argument is passed by value, but not consumed in the function body
 --> src/main.rs:7:15
  |
7 | fn bar<O>(_o: O) {}
  |               ^ help: consider taking a reference instead: `&O`
  |
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#needless_pass_by_value

warning: this argument is passed by value, but not consumed in the function body
 --> src/main.rs:9:29
  |
9 | pub fn run<T: O>(o: T, alg: Algorithm) {
  |                             ^^^^^^^^^
  |
help: consider marking this type as Copy
 --> src/main.rs:1:1
  |
1 | / pub enum Algorithm {
2 | |     Scalar,
3 | |     Simd,
4 | | }
  | |_^
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#needless_pass_by_value
help: consider taking a reference instead
  |
9 | pub fn run<T: O>(o: T, alg: &Algorithm) {
10|     match *alg {
  |

but it appears to me that both errors are incorrect? o is consumed in the body, in the calls to foo and bar, and alg is consumed by the match ? Or what am I missing?

Or is the lint telling me that the intent of foo, bar, and run might not be to drop o and alg ? None of these types is Copy, so from the API signature, that's pretty much what the intent appears to be to me.

Making this change requires you to also change all callers of these functions to pass a reference &, or to make the types implement Copy which would break the use case where the intent is to ensure that they are used once, so these are not very clear cut suggestions either.

So what's the intent of this lint ? It doesn't appear to be to catch any bugs, because all possible issues would be compiler errors, and the code has to compile before clippy can be called (typically).

So maybe it is to improve APIs, but then this lint should only apply to pub exported items or something, and even then, I am skeptical.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions