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 with moved data #5595

Closed
Jonathas-Conceicao opened this issue May 13, 2020 · 2 comments · Fixed by #7011
Closed

redundant-clone: False positive with moved data #5595

Jonathas-Conceicao opened this issue May 13, 2020 · 2 comments · Fixed by #7011
Labels
C-bug Category: Clippy is not doing the correct thing E-hard Call for participation: This a hard problem and requires more experience or effort to work on I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Jonathas-Conceicao
Copy link

Jonathas-Conceicao commented May 13, 2020

I have made the following small example to illustrate a warning of redudant-clone I've been getting:

#[derive(Clone)]
struct NoCopy;

fn foo<F: Fn()>(_: &NoCopy, _: F) {}

fn main() {
    let data = NoCopy;
    foo(&data.clone(), move || {
        let _ = data;
    });
}

Which gives me:

warning: redundant clone
 --> src/main.rs:8:14
  |
8 |     foo(&data.clone(), move || {
  |              ^^^^^^^^ help: remove this
  |
  = note: `#[warn(clippy::redundant_clone)]` on by default
note: cloned value is neither consumed nor mutated
 --> src/main.rs:8:10
  |
8 |     foo(&data.clone(), move || {
  |          ^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

warning: 1 warning emitted

However, removing the clone would result in a error as the data will be moved to the closure while still borrowed as an argument to foo. Am I right to think this is a false positive, or am I messing something to fix this?

$ cargo clippy -V
clippy 0.0.212 (769d12e 2020-05-12)
@flip1995 flip1995 added E-hard Call for participation: This a hard problem and requires more experience or effort to work on C-bug Category: Clippy is not doing the correct thing labels May 14, 2020
@matthiaskrgr
Copy link
Member

ping @sinkuu ?

@sinkuu
Copy link
Contributor

sinkuu commented May 18, 2020

Minimized:

#[derive(Clone)]
struct NoCopy;

fn main() {
    let data = NoCopy;
    (&data.clone(), data);
}

This bug was introduced in #5304. It has to be made sure that the temporary value (data.clone()) is killed before uses of the cloned value (data).

Jonathas-Conceicao pushed a commit to UpdateHub/updatehub that referenced this issue Jun 16, 2020
tracking issue: rust-lang/rust-clippy#5595

Signed-off-by: Jonathas-Conceicao <jonathas.conceicao@ossystems.com.br>
otavio pushed a commit to UpdateHub/updatehub that referenced this issue Jun 16, 2020
tracking issue: rust-lang/rust-clippy#5595

Signed-off-by: Jonathas-Conceicao <jonathas.conceicao@ossystems.com.br>
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 21, 2020
@bors bors closed this as completed in 92c4fc3 Apr 1, 2021
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 E-hard Call for participation: This a hard problem and requires more experience or effort to work on 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.

5 participants