rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Clippy demands you violate ownership to satisfy `assigning_clones`

Open workingjubilee opened this issue 1 year ago • 3 comments

Summary

I believe this isn't checking to see if it's in any kind of looping structure which makes this kind of lint extremely fallible:

   Checking pgrx-sql-entity-graph v0.12.0-alpha.1 (/home/jubilee/tcdi/pgrx/pgrx-sql-entity-graph)
warning: failed to automatically apply fixes suggested by rustc to crate `pgrx_sql_entity_graph`

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

  * pgrx-sql-entity-graph/src/pg_extern/returning.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 `segments` as mutable because it is also borrowed as immutable
   --> pgrx-sql-entity-graph/src/pg_extern/returning.rs:120:29
    |
107 |                         if let Some(segment) = segments.filter_last_ident("Option") {
    |                                                -------- immutable borrow occurs here
...
120 |                             segments.clone_from(&this_path.path.segments); // recurse deeper
    |                             ^^^^^^^^^----------^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                             |        |
    |                             |        immutable borrow later used by call
    |                             mutable borrow occurs here

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 `Clone::clone()` may be inefficient
   --> pgrx-sql-entity-graph/src/pg_extern/returning.rs:120:29
    |
120 | ...                   segments = this_path.path.segments.clone(); // recurse deeper
    |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_from()`: `segments.clone_from(&this_path.path.segments)`
    |
    = 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: `pgrx-sql-entity-graph` (lib test) generated 1 warning (run `cargo clippy --fix --lib -p pgrx-sql-entity-graph --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `pgrx_sql_entity_graph`

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

  * pgrx-sql-entity-graph/src/pg_extern/returning.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 `segments` as mutable because it is also borrowed as immutable
   --> pgrx-sql-entity-graph/src/pg_extern/returning.rs:120:29
    |
107 |                         if let Some(segment) = segments.filter_last_ident("Option") {
    |                                                -------- immutable borrow occurs here
...
120 |                             segments.clone_from(&this_path.path.segments); // recurse deeper
    |                             ^^^^^^^^^----------^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                             |        |
    |                             |        immutable borrow later used by call
    |                             mutable borrow occurs here

error: aborting due to 1 previous error

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

warning: `pgrx-sql-entity-graph` (lib) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.20s

Reproducer

The source is here:

https://github.com/pgcentralfoundation/pgrx/blob/beb79011a377bb96c34fcc50ae2d652aa1419eb5/pgrx-sql-entity-graph/src/pg_extern/returning.rs#L107-L120

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

workingjubilee avatar May 03 '24 00:05 workingjubilee

May duplicate https://github.com/rust-lang/rust-clippy/pull/12756

workingjubilee avatar May 03 '24 00:05 workingjubilee

May duplicate #12756

Looks like that fixes it indeed. I ran a clippy build with those changes on the pgrx-sql-entity-graph crate and it no longer shows that warning.

y21 avatar May 03 '24 00:05 y21

thanks!

workingjubilee avatar May 03 '24 01:05 workingjubilee