Skip to content

False redundant_closure_call for a closure that has multiple calls #3354

Closed
@cuviper

Description

@cuviper
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions