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

redundant_clone: False positive when original value is only used in macro #10545

Open
taiki-e opened this issue Mar 25, 2023 · 3 comments
Open
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

Comments

@taiki-e
Copy link
Member

taiki-e commented Mar 25, 2023

Summary

redundant_clone warns when both the cloned and modified value and the original value are used in a macro, but should not warn because the cloned value has been modified.

Lint Name

redundant_clone

Reproducer

I tried this code:

#![warn(clippy::redundant_clone)]

use quote::quote;
use proc_macro2::TokenStream;

pub fn f(args: TokenStream, function1: syn::ItemFn) -> TokenStream {
    let mut function2 = function1.clone();
    function2.sig.constness = None; // modify function2

    quote! {
        #[cfg(not(#args))]
        #function2 // fn ...
        #[cfg(#args)]
        #function1 // const fn ...
    }
}

playground

I saw this happen:

warning: redundant clone
 --> src/lib.rs:7:34
  |
7 |     let mut function2 = function1.clone();
  |                                  ^^^^^^^^ help: remove this
  |
note: cloned value is neither consumed nor mutated
 --> src/lib.rs:7:25
  |
7 |     let mut function2 = function1.clone();
  |                         ^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::redundant_clone)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^

I expected to see this happen: no warning

Version

rustc 1.70.0-nightly (8be3c2bda 2023-03-24)
binary: rustc
commit-hash: 8be3c2bda6b683f87b24714ba595e8b04faef54c
commit-date: 2023-03-24
host: aarch64-apple-darwin
release: 1.70.0-nightly
LLVM version: 15.0.7

Additional Labels

No response

@taiki-e taiki-e added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 25, 2023
taiki-e added a commit to taiki-e/syn-mid that referenced this issue Mar 25, 2023
rust-lang/rust-clippy#10545

```
warning: redundant clone
  --> examples/const_fn/src/lib.rs:25:38
   |
25 |     let mut function = const_function.clone();
   |                                      ^^^^^^^^ help: remove this
   |
note: cloned value is neither consumed nor mutated
  --> examples/const_fn/src/lib.rs:25:24
   |
25 |     let mut function = const_function.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
```
@jbr
Copy link

jbr commented Apr 3, 2023

Just ran into the same with this usage

@ryoqun
Copy link

ryoqun commented May 18, 2023

okay, pardon for repeated cross posts. but i confirmed this issue's regression window is same as #10577 (comment) and #10517 (comment)

so, i think all are affected by a same root cause?

@mkrasnitski
Copy link
Contributor

Ran into this issue on stable in the serenity library with the following two usages: 1, 2.

bors added a commit that referenced this issue Jun 5, 2023
Move `redundant_clone` to `nursery`

changelog: [`redundant_clone`]: Move to `nursery`

A bunch of FPs in `redundant_clone` have sprung up after upstream MIR changes: rust-lang/rust#108944

- #10870
- #10577
- #10545
- #10517

r? `@flip1995`
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants