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

False positive for clone_on_copy #4384

Closed
vallentin opened this issue Aug 14, 2019 · 0 comments · Fixed by #4411
Closed

False positive for clone_on_copy #4384

vallentin opened this issue Aug 14, 2019 · 0 comments · Fixed by #4411
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@vallentin
Copy link
Contributor

I've encountered a false positive, when calling .clone() on a field of a struct. Followed by calling a method (&self) that takes a closure, which borrows the original struct.

I encountered it using the glfw crate, and boils down to the following:

// glfw: Glfw + Copy
window.glfw.clone().with_primary_monitor(|_, _| {
    println!("{:?}", window.get_size());
});

Clippy suggests removing .clone() since Glfw implements Copy.

using clone on a Copy type

help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
help: try removing the clone call: window.glfwclippy(clippy::clone_on_copy)

However, that doesn't work, since it would reborrow window.

error[E0502]: cannot borrow window.glfw as mutable because it is also borrowed as immutable


Here's two minimal, reproducible examples.

Example 1

let mut i = 0;

fn foo<F>(_: &i32, mut f: F) where F: FnMut() {
    f();
}

foo(&i.clone(), || {
    i += 1;
});

println!("{}", i);

Example 2

#[derive(Clone, Copy, Debug)]
struct Foo(i32);

impl Foo {
    fn bar<F>(&self, mut f: F) where F: FnMut() {
        f();
    }
}

let mut foo = Foo(0);

foo.clone().bar(|| {
    foo.0 += 1;
});

println!("{:?}", foo);

I'm aware that changing &self to self and removing .clone() works. However, that would require me to modify glfw.

Version: clippy 0.0.212 (082cfa7 2019-06-07)

@flip1995 flip1995 added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Aug 15, 2019
bors added a commit that referenced this issue Aug 19, 2019
Fix `clone_on_copy` false positives

Closes #4384

changelog: Fix `clone_on_copy` false positives
@bors bors closed this as completed in 68a1af5 Aug 19, 2019
chansuke pushed a commit to chansuke/rust-clippy that referenced this issue Aug 19, 2019
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants