Skip to content

needless_pass_by_value and closures #2434

Open
@phrohdoh

Description

@phrohdoh

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-correctnessLint: Belongs in the correctness lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions