Skip to content

FP redundant_closure #5939

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

Closed
matthiaskrgr opened this issue Aug 21, 2020 · 2 comments · Fixed by #8983
Closed

FP redundant_closure #5939

matthiaskrgr opened this issue Aug 21, 2020 · 2 comments · Fixed by #8983
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};

pub fn main() {
    let path = &PathBuf::from("a");
    do_op(path, "remove file", |p| std::fs::remove_file(p)); // redundant closure
}

fn do_op<F>(path: &Path, _desc: &str, mut f: F)
where
    F: FnMut(&Path) -> io::Result<()>,
{
    match f(path) {
        Ok(()) => {}
        Err(ref _e) => {}
        Err(_e) => {}
    }
}

Clippy suggests removing the marked closure, to std::fs::remove_file but this causes a compiler error:

error[E0308]: mismatched types
 --> src/main.rs:6:5
  |
6 |     do_op(path, "remove file", std::fs::remove_file);
  |     ^^^^^ one type is more general than the other
  |
  = note: expected type `std::ops::FnOnce<(&std::path::Path,)>`
             found type `std::ops::FnOnce<(&std::path::Path,)>`

Code found in rustc bootstrap crate.
clippy 0.0.212 (e15510c 2020-08-20)

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Aug 21, 2020
@DutchGhost
Copy link

I hit this as well with pico_args for argument parsing,

#[derive(Debug)]
struct Cli {
    config: PathBuf,
}

impl Cli {
    fn from_args(mut args: pico_args::Arguments) -> Result<Self, BoxedError> {
        let config = args.value_from_fn("--config", |s| PathBuf::try_from(s))?;

        Ok(Self { config })
    }
}

Clippy suggest PathBuf::try_from over the closure, resulting in the same error as the post above

@matthiaskrgr matthiaskrgr added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 18, 2020
@ThibsG
Copy link
Contributor

ThibsG commented Jan 14, 2021

Looks like the same as #5594

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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants