Skip to content

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

Open
@taiki-e

Description

@taiki-e

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions