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: 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

Closed
gibfahn opened this issue Mar 11, 2024 · 2 comments · Fixed by #12756
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@gibfahn
Copy link
Contributor

gibfahn commented Mar 11, 2024

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

@gibfahn gibfahn added the C-bug Category: Clippy is not doing the correct thing label Mar 11, 2024
@gibfahn gibfahn changed the title [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 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 Mar 11, 2024
@gibfahn
Copy link
Contributor Author

gibfahn commented Mar 11, 2024

This might be a duplicate of #12444 (missed that before opening this) , cc/ @Kobzol

@y21 y21 added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Mar 11, 2024
@Kobzol
Copy link
Contributor

Kobzol commented Mar 12, 2024

Yes, it's most probably the same issue, thanks.

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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
3 participants