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

assigning_clones: suggestion causes error because of borrowed target #12749

Open
samueltardieu opened this issue May 2, 2024 · 1 comment · May be fixed by #12756
Open

assigning_clones: suggestion causes error because of borrowed target #12749

samueltardieu opened this issue May 2, 2024 · 1 comment · May be fixed by #12756
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

@samueltardieu
Copy link
Contributor

Summary

When the entity being cloned borrows the former content of the target, the target cannot be replaced using .clone_into().

Reproducer

I tried this code:

use std::path::PathBuf;

#[allow(unused)]
fn main() {
    let mut path = PathBuf::from("/a/b/c");
    path = path.components().as_path().to_owned();
}

I expected to see this happen: no suggestion

Instead, this happened:

warning: assigning the result of `ToOwned::to_owned()` may be inefficient
 --> src/main.rs:6:5
  |
6 |     path = path.components().as_path().to_owned();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `path.components().as_path().clone_into(&mut path)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assigning_clones
  = note: `#[warn(clippy::assigning_clones)]` on by default

Applying the suggestion then leads to:

error[E0502]: cannot borrow `path` as mutable because it is also borrowed as immutable
 --> src/main.rs:6:44
  |
6 |     path.components().as_path().clone_into(&mut path);
  |     ----                        ---------- ^^^^^^^^^ mutable borrow occurs here
  |     |                           |
  |     |                           immutable borrow later used by call
  |     immutable borrow occurs here

Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Additional Labels

@rustbot label I-suggestion-causes-error

@samueltardieu samueltardieu added the C-bug Category: Clippy is not doing the correct thing label May 2, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label May 2, 2024
@samueltardieu
Copy link
Contributor Author

Found in uutils/coreutils#6330

@samueltardieu samueltardieu changed the title assigning_clones: suggestion causes error assigning_clones: suggestion causes error because of borrowed target May 2, 2024
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
2 participants