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

Clippy reports a false error #11770

Closed
Makogan opened this issue Nov 7, 2023 · 1 comment
Closed

Clippy reports a false error #11770

Makogan opened this issue Nov 7, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@Makogan
Copy link

Makogan commented Nov 7, 2023

Summary

when running clippy it suggests to change a snippet which will result in non-compilation. It suggests that removing a clone is a better choice because the value is not consumed or mutated, and yet if the clone is removed the code won;t compile as the value is actually being mutated.

Reproducer

I tried this code:

pub fn conjugate_gradient(
    a: &CsrMatrix<f64>,
    threshold: f64,
    solution: &mut DVector<f64>,
) -> usize {
    let mut r = &*solution - a * &*solution;
    let mut iter_count = 0;
    let mut p = r.clone();

    loop {
        let alpha = r.dot(&r) / p.dot(&(a * &p));

        *solution = &*solution + alpha * &p;
        let r_next = &r - alpha * a * &p;

        let delta = r_next.norm_squared();
        debug_assert!(!delta.is_nan());

        if delta <= threshold {
            break;
        }

        let beta = r_next.dot(&r_next) / r.dot(&r);
        p = &r_next + beta * &p;
        r = r_next;

        iter_count += 1;

        // Arbitrary iteration count break.
        if iter_count > 1000 {
            break;
        }
    }

    iter_count
}

I expected to see this happen:
The clone is necessary, clippy should not suggest anything.

Instead, this happened:

warning: redundant clone
   --> crates/fse/src/classifier/classifier_rbf.rs:319:18
    |
319 |     let mut p = r.clone();
    |                  ^^^^^^^^ help: remove this
    |
note: cloned value is neither consumed nor mutated
   --> crates/fse/src/classifier/classifier_rbf.rs:319:17
    |
319 |     let mut p = r.clone();
    |                 ^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
    = note: `#[warn(clippy::redundant_clone)]` on by default

You can clearly see both p and r mutate over the algorithm.

Version

rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2

Additional Labels

No response

@Makogan Makogan added the C-bug Category: Clippy is not doing the correct thing label Nov 7, 2023
@Jarcho
Copy link
Contributor

Jarcho commented Nov 7, 2023

Duplicate of #10577. redundant_clone is currently broken and in the nursery.

@Jarcho Jarcho closed this as completed Nov 7, 2023
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

No branches or pull requests

2 participants