Skip to content

assigning_clones: after fixes were automatically applied the compiler reported errors within these files: – cannot borrow name as mutable because it is also borrowed as immutable #12460

@gibfahn

Description

@gibfahn

Summary

Since upgrading to the latest nightly I'm seeing this error in my project. I think this was caused by the new lint added in #12077

Output from the sample file included below:

cargo +nightly clippy --color=always --fix --allow-staged
    Checking t-rs v0.1.0 (/tmp/t-rs)
warning: failed to automatically apply fixes suggested by rustc to crate `t_rs`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0502]: cannot borrow `name` as mutable because it is also borrowed as immutable
 --> src/main.rs:5:38
  |
4 |         if let Some(stripped_name) = name.strip_prefix("baz-") {
  |                                      ---- immutable borrow occurs here
5 |             stripped_name.clone_into(&mut name);
  |                           ---------- ^^^^^^^^^ mutable borrow occurs here
  |                           |
  |                           immutable borrow later used by call

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0502`.
Original diagnostics will follow.

warning: assigning the result of `ToOwned::to_owned()` may be inefficient
 --> src/main.rs:5:13
  |
5 |             name = stripped_name.to_owned();
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `stripped_name.clone_into(&mut name)`
  |
  = 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

warning: `t-rs` (bin "t-rs") generated 1 warning (run `cargo clippy --fix --bin "t-rs"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `t_rs`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0502]: cannot borrow `name` as mutable because it is also borrowed as immutable
 --> src/main.rs:5:38
  |
4 |         if let Some(stripped_name) = name.strip_prefix("baz-") {
  |                                      ---- immutable borrow occurs here
5 |             stripped_name.clone_into(&mut name);
  |                           ---------- ^^^^^^^^^ mutable borrow occurs here
  |                           |
  |                           immutable borrow later used by call

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0502`.
Original diagnostics will follow.

warning: `t-rs` (bin "t-rs" test) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s

Reproducer

I tried this code:

fn main() {
    let mut name = "foo".to_owned();
    if name.starts_with("bar") {
        if let Some(stripped_name) = name.strip_prefix("baz-") {
            name = stripped_name.to_owned();
        }
    }
    println!("Name: {name}");
}

And I ran:

cargo +nightly clippy --color=always --fix --allow-staged

I expected it to not error, instead I got the output at the top of this issue.

Version

rustc 1.78.0-nightly (2d24fe591 2024-03-09)
binary: rustc
commit-hash: 2d24fe591f30386d6d5fc2bb941c78d7266bf10f
commit-date: 2024-03-09
host: aarch64-apple-darwin
release: 1.78.0-nightly
LLVM version: 18.1.0

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