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

False redundant_closure_call for a closure that has multiple calls #3354

Closed
cuviper opened this issue Oct 23, 2018 · 1 comment · Fixed by #5800
Closed

False redundant_closure_call for a closure that has multiple calls #3354

cuviper opened this issue Oct 23, 2018 · 1 comment · Fixed by #5800
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@cuviper
Copy link
Member

cuviper commented Oct 23, 2018

pub fn sqrt(n: u32) -> u32 {
    let mut x;
    let next = move |x: u32| (x + n / x) >> 1;

    x = next(1);
    loop {
        let xn = next(x);
        if x <= xn {
            return x;
        }
        x = xn;
    }
}

Note that next is called in two places, but it gets a warning that it's called just once:

$ cargo +nightly clippy -V
clippy 0.0.212 (5afdf8b 2018-10-14)
$ cargo +nightly clippy
    Checking sqrt v0.1.0 (/tmp/sqrt)
warning: Closure called just once immediately after it was declared
 --> src/lib.rs:5:5
  |
5 |     x = next(1);
  |     ^^^^^^^^^^^
  |
  = note: #[warn(clippy::redundant_closure_call)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#redundant_closure_call

    Finished dev [unoptimized + debuginfo] target(s) in 0.47s

If I move next to be declared first, the warning goes away.

@athre0z
Copy link

athre0z commented Sep 20, 2019

Different code, same issue:

pub fn stuff(arg1: &mut f64, arg2: &mut f64) {
    let closure = || 123.;
    *arg1 = closure();
    *arg2 = closure();
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fe094d631b9db369cb4e6a13f2889527

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants