Skip to content
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

needless_pass_by_value and closures #2434

Open
phrohdoh opened this issue Feb 4, 2018 · 2 comments
Open

needless_pass_by_value and closures #2434

phrohdoh opened this issue Feb 4, 2018 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group

Comments

@phrohdoh
Copy link

phrohdoh commented Feb 4, 2018

Given this example code:

extern crate clap;
use clap::{App, Arg};

fn my_validator(v: String) -> Result<(), String> {
    if v.len() > 5 {
        Err("no".into())
    } else {
        Ok(())
    }
}

fn main() {
    let matches = App::new("example")
        .arg(Arg::with_name("myarg")
            .validator(my_validator))
        .get_matches();

    println!("{:?}", matches);
}

Clippy suggests changing v: String to v: &str but my_validator is given to Arg::validator which requires String.

warning: this argument is passed by value, but not consumed in the function body
 --> src/main.rs:4:20
  |
4 | fn my_validator(v: String) -> Result<(), String> {
  |                    ^^^^^^ help: consider changing the type to: `&str`
  |
  = note: #[warn(needless_pass_by_value)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.185/index.html#needless_pass_by_value
error[E0631]: type mismatch in function arguments
  --> src/main.rs:15:14
   |
4  | fn my_validator(v: &str) -> Result<(), String> {
   | ---------------------------------------------- found signature of `for<'r> fn(&'r str) -> _`
...
15 |             .validator(my_validator))
   |              ^^^^^^^^^ expected signature of `fn(std::string::String) -> _`

error: aborting due to previous error

Could clippy investigate the function usage in situations like this to prevent suggestions such as this?

@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group labels Nov 21, 2018
@dtolnay
Copy link
Member

dtolnay commented Nov 21, 2018

Notes from @oli-obk in #1939:

We need to check all use sites and technically the moment the function is public we need to bail out, too, because external users' code will be broken by it.

I think this will make the lint much less useful, but I also don't see how to improve it.

@camsteffen
Copy link
Contributor

This particular case is fixed in Clap v3 here (still in beta though).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
Development

No branches or pull requests

3 participants