1
Fork 0
rust/compiler/rustc_borrowck/src
Esteban Küber cc80106cb5 Provide more suggestions for cloning immutable bindings
When encountering multiple mutable borrows, suggest cloning and adding
derive annotations as needed.

```
error[E0596]: cannot borrow `sm.x` as mutable, as it is behind a `&` reference
  --> $DIR/accidentally-cloning-ref-borrow-error.rs:32:9
   |
LL |     foo(&mut sm.x);
   |         ^^^^^^^^^ `sm` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: `Str` doesn't implement `Clone`, so this call clones the reference `&Str`
  --> $DIR/accidentally-cloning-ref-borrow-error.rs:31:21
   |
LL |     let mut sm = sr.clone();
   |                     ^^^^^^^
help: consider annotating `Str` with `#[derive(Clone)]`
   |
LL + #[derive(Clone)]
LL | struct Str {
   |
help: consider specifying this binding's type
   |
LL |     let mut sm: &mut Str = sr.clone();
   |               ++++++++++
```

```
error[E0596]: cannot borrow `*inner` as mutable, as it is behind a `&` reference
  --> $DIR/issue-91206.rs:14:5
   |
LL |     inner.clear();
   |     ^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: you can `clone` the `Vec<usize>` value and consume it, but this might not be your desired behavior
  --> $DIR/issue-91206.rs:11:17
   |
LL |     let inner = client.get_inner_ref();
   |                 ^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying this binding's type
   |
LL |     let inner: &mut Vec<usize> = client.get_inner_ref();
   |              +++++++++++++++++
```
2023-12-04 21:54:34 +00:00
..
constraints Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
diagnostics Provide more suggestions for cloning immutable bindings 2023-12-04 21:54:34 +00:00
polonius move remaining legacy polonius fact generation out of NLL module 2023-11-26 13:03:32 +00:00
region_infer move and maintain live loans in LivenessValues 2023-12-01 14:04:54 +00:00
type_check Auto merge of #118470 - nnethercote:cleanup-error-handlers, r=compiler-errors 2023-12-02 02:48:34 +00:00
util Move some utils out of rustc_const_eval 2023-04-16 12:05:54 +02:00
borrow_set.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
borrowck_errors.rs Use targetted diagnostic for borrow across yield error 2023-10-27 13:05:49 +00:00
consumers.rs Fix redundant explicit link in rustc_borrowck 2023-08-19 02:22:13 +08:00
dataflow.rs improve NLL/polonius scope equality assertion 2023-12-01 14:04:54 +00:00
def_use.rs rename BorrowKind::Shallow to Fake 2023-11-08 22:55:28 +01:00
facts.rs introduce Polonius enum for -Zpolonius 2023-10-04 16:16:12 +00:00
lib.rs Auto merge of #118470 - nnethercote:cleanup-error-handlers, r=compiler-errors 2023-12-02 02:48:34 +00:00
location.rs Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
member_constraints.rs remove unused muts 2023-04-28 20:19:48 +02:00
nll.rs Auto merge of #118470 - nnethercote:cleanup-error-handlers, r=compiler-errors 2023-12-02 02:48:34 +00:00
path_utils.rs Remove Upvar duplication 2023-11-26 13:19:10 -05:00
place_ext.rs use PlaceRef abstractions more consistently 2023-06-25 20:38:01 -04:00
places_conflict.rs rename BorrowKind::Shallow to Fake 2023-11-08 22:55:28 +01:00
prefixes.rs Add docs, remove code, change subtyper code 2023-10-02 23:39:44 +03:00
renumber.rs add fixme to RegionCtxt 2023-11-13 14:13:54 +00:00
session_diagnostics.rs s/generator/coroutine/ 2023-10-20 21:14:01 +00:00
universal_regions.rs Rollup merge of #118302 - mu001999:dead_code/clean, r=cjgillot 2023-11-26 15:44:54 +01:00
used_muts.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00