rust/tests/ui/unboxed-closures
bors 6eaa7fb576 Auto merge of #122603 - estebank:clone-o-rama, r=lcnr
Detect borrow checker errors where `.clone()` would be an appropriate user action

When a value is moved twice, suggest cloning the earlier move:

```
error[E0509]: cannot move out of type `U2`, which implements the `Drop` trait
  --> $DIR/union-move.rs:49:18
   |
LL |         move_out(x.f1_nocopy);
   |                  ^^^^^^^^^^^
   |                  |
   |                  cannot move out of here
   |                  move occurs because `x.f1_nocopy` has type `ManuallyDrop<RefCell<i32>>`, which does not implement the `Copy` trait
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL |         move_out(x.f1_nocopy.clone());
   |                             ++++++++
```

When a value is borrowed by an `fn` call, consider if cloning the result of the call would be reasonable, and suggest cloning that, instead of the argument:

```
error[E0505]: cannot move out of `a` because it is borrowed
  --> $DIR/variance-issue-20533.rs:53:14
   |
LL |         let a = AffineU32(1);
   |             - binding `a` declared here
LL |         let x = bat(&a);
   |                     -- borrow of `a` occurs here
LL |         drop(a);
   |              ^ move out of `a` occurs here
LL |         drop(x);
   |              - borrow later used here
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL |         let x = bat(&a).clone();
   |                        ++++++++
```

otherwise, suggest cloning the argument:

```
error[E0505]: cannot move out of `a` because it is borrowed
  --> $DIR/variance-issue-20533.rs:59:14
   |
LL |         let a = ClonableAffineU32(1);
   |             - binding `a` declared here
LL |         let x = foo(&a);
   |                     -- borrow of `a` occurs here
LL |         drop(a);
   |              ^ move out of `a` occurs here
LL |         drop(x);
   |              - borrow later used here
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL -         let x = foo(&a);
LL +         let x = foo(a.clone());
   |
```

This suggestion doesn't attempt to square out the types between what's cloned and what the `fn` expects, to allow the user to make a determination on whether to change the `fn` call or `fn` definition themselves.

Special case move errors caused by `FnOnce`:

```
error[E0382]: use of moved value: `blk`
  --> $DIR/once-cant-call-twice-on-heap.rs:8:5
   |
LL | fn foo<F:FnOnce()>(blk: F) {
   |                    --- move occurs because `blk` has type `F`, which does not implement the `Copy` trait
LL |     blk();
   |     ----- `blk` moved due to this call
LL |     blk();
   |     ^^^ value used here after move
   |
note: `FnOnce` closures can only be called once
  --> $DIR/once-cant-call-twice-on-heap.rs:6:10
   |
LL | fn foo<F:FnOnce()>(blk: F) {
   |          ^^^^^^^^ `F` is made to be an `FnOnce` closure here
LL |     blk();
   |     ----- this value implements `FnOnce`, which causes it to be moved when called
```

Account for redundant `.clone()` calls in resulting suggestions:

```
error[E0507]: cannot move out of dereference of `S`
  --> $DIR/needs-clone-through-deref.rs:15:18
   |
LL |         for _ in self.clone().into_iter() {}
   |                  ^^^^^^^^^^^^ ----------- value moved due to this method call
   |                  |
   |                  move occurs because value has type `Vec<usize>`, which does not implement the `Copy` trait
   |
note: `into_iter` takes ownership of the receiver `self`, which moves value
  --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: you can `clone` the value and consume it, but this might not be your desired behavior
   |
LL |         for _ in <Vec<usize> as Clone>::clone(&self).into_iter() {}
   |                  ++++++++++++++++++++++++++++++    ~
```

We use the presence of `&mut` values in a move error as a proxy for the user caring about side effects, so we don't emit a clone suggestion in that case:

```
error[E0505]: cannot move out of `s` because it is borrowed
  --> $DIR/borrowck-overloaded-index-move-index.rs:53:7
   |
LL |     let mut s = "hello".to_string();
   |         ----- binding `s` declared here
LL |     let rs = &mut s;
   |              ------ borrow of `s` occurs here
...
LL |     f[s] = 10;
   |       ^ move out of `s` occurs here
...
LL |     use_mut(rs);
   |             -- borrow later used here
```

We properly account for `foo += foo;` errors where we *don't* suggest `foo.clone() += foo;`, instead suggesting `foo += foo.clone();`.

---

Each commit can be reviewed in isolation. There are some "cleanup" commits, but kept them separate in order to show *why* specific changes were being made, and their effect on tests' output.

Fix #49693, CC #64167.
2024-04-13 09:07:26 +00:00
..
auxiliary
issue-18652.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-18661.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-30906.rs
issue-30906.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-53448.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
non-tupled-arg-mismatch.rs Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
non-tupled-arg-mismatch.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
non-tupled-call.rs
non-tupled-call.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
type-id-higher-rank.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closure-feature-gate.rs
unboxed-closure-feature-gate.stderr Bless tests 2024-01-13 12:46:58 -05:00
unboxed-closure-illegal-move.rs
unboxed-closure-illegal-move.stderr Better account for more cases involving closures 2024-04-12 04:46:31 +00:00
unboxed-closure-immutable-capture.rs
unboxed-closure-immutable-capture.stderr
unboxed-closure-no-cyclic-sig.rs s/generator/coroutine/ 2023-10-20 21:14:01 +00:00
unboxed-closure-no-cyclic-sig.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-region.rs
unboxed-closure-region.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-sugar-default.rs
unboxed-closure-sugar-default.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-sugar-equiv.rs
unboxed-closure-sugar-equiv.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-sugar-lifetime-elision.rs
unboxed-closure-sugar-lifetime-elision.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-sugar-not-used-on-fn.rs
unboxed-closure-sugar-not-used-on-fn.stderr Bless tests 2024-01-13 12:46:58 -05:00
unboxed-closure-sugar-region.rs make type_flags(ReError) & HAS_ERROR 2024-03-20 17:29:58 +00:00
unboxed-closure-sugar-region.stderr make type_flags(ReError) & HAS_ERROR 2024-03-20 17:29:58 +00:00
unboxed-closure-sugar-used-on-struct-1.rs
unboxed-closure-sugar-used-on-struct-1.stderr
unboxed-closure-sugar-used-on-struct-3.rs
unboxed-closure-sugar-used-on-struct-3.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-sugar-used-on-struct.rs
unboxed-closure-sugar-used-on-struct.stderr
unboxed-closure-sugar-wrong-number-number-type-parameters-1.rs
unboxed-closure-sugar-wrong-number-number-type-parameters-1.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closure-sugar-wrong-number-number-type-parameters-3.rs
unboxed-closure-sugar-wrong-number-number-type-parameters-3.stderr
unboxed-closure-sugar-wrong-number-number-type-parameters.rs Avoid silencing relevant follow-up errors 2024-01-09 21:08:16 +00:00
unboxed-closure-sugar-wrong-number-number-type-parameters.stderr Avoid silencing relevant follow-up errors 2024-01-09 21:08:16 +00:00
unboxed-closure-sugar-wrong-trait.rs
unboxed-closure-sugar-wrong-trait.stderr
unboxed-closures-all-traits.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-blanket-fn-mut.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-blanket-fn.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-borrow-conflict.rs
unboxed-closures-borrow-conflict.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-boxed.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-by-ref.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-call-fn-autoderef.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-call-sugar-autoderef.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-call-sugar-object-autoderef.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-call-sugar-object.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-counter-not-moved.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-counter-not-moved.stderr
unboxed-closures-cross-crate.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-direct-sugary-call.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-drop.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-extern-fn-hr.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-extern-fn.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-failed-recursive-fn-1.rs
unboxed-closures-failed-recursive-fn-1.stderr
unboxed-closures-failed-recursive-fn-2.rs
unboxed-closures-failed-recursive-fn-2.stderr Use fn ptr signature instead of {closure@..} in infer error 2024-04-10 00:41:27 +00:00
unboxed-closures-fn-as-fnmut-and-fnonce.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-fnmut-as-fn.rs
unboxed-closures-fnmut-as-fn.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-fnmut-as-fnonce.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-generic.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-arg-types-from-expected-bound.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-arg-types-from-expected-object-type.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-arg-types-w-bound-regs-from-expected-bound.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-argument-types-two-region-pointers.rs
unboxed-closures-infer-argument-types-two-region-pointers.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-infer-explicit-call-early.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-fn-once-move-from-projection.rs
unboxed-closures-infer-fn-once-move-from-projection.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-infer-fnmut-calling-fnmut-no-mut.rs
unboxed-closures-infer-fnmut-calling-fnmut-no-mut.stderr
unboxed-closures-infer-fnmut-calling-fnmut.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-fnmut-missing-mut.rs
unboxed-closures-infer-fnmut-missing-mut.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-infer-fnmut-move-missing-mut.rs
unboxed-closures-infer-fnmut-move-missing-mut.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-infer-fnmut-move.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-fnmut.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-fnonce-call-twice.rs
unboxed-closures-infer-fnonce-call-twice.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-infer-fnonce-move-call-twice.rs
unboxed-closures-infer-fnonce-move-call-twice.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-infer-fnonce-move.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-fnonce.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-kind.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-recursive-fn.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-infer-upvar.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-manual-impl.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-monomorphization.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-move-from-projection-issue-30046.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-move-mutable.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-move-mutable.stderr
unboxed-closures-move-some-upvars-in-by-ref-closure.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-mutate-upvar.rs
unboxed-closures-mutate-upvar.stderr
unboxed-closures-mutated-upvar-from-fn-closure.rs
unboxed-closures-mutated-upvar-from-fn-closure.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-prelude.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-recursive-fn-using-fn-mut.rs
unboxed-closures-recursive-fn-using-fn-mut.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-simple.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-single-word-env.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-static-call-fn-once.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-static-call-wrong-trait.rs
unboxed-closures-static-call-wrong-trait.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unboxed-closures-sugar-object.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-type-mismatch-closure-from-another-scope.rs On type error involving closure, avoid ICE 2023-10-12 23:29:02 +00:00
unboxed-closures-type-mismatch-closure-from-another-scope.stderr Don't suggest deref macro since it's unstable 2024-03-21 11:42:49 -04:00
unboxed-closures-type-mismatch.rs On type error of closure call argument, point at earlier calls that affected inference 2023-09-28 22:04:15 +00:00
unboxed-closures-type-mismatch.stderr On type error of closure call argument, point at earlier calls that affected inference 2023-09-28 22:04:15 +00:00
unboxed-closures-unique-type-id.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unboxed-closures-unsafe-extern-fn.rs
unboxed-closures-unsafe-extern-fn.stderr Pretty print Fn traits in rustc_on_unimplemented 2023-11-02 20:57:05 +00:00
unboxed-closures-wrong-abi.rs
unboxed-closures-wrong-abi.stderr Pretty print Fn traits in rustc_on_unimplemented 2023-11-02 20:57:05 +00:00
unboxed-closures-wrong-arg-type-extern-fn.rs
unboxed-closures-wrong-arg-type-extern-fn.stderr Pretty print Fn traits in rustc_on_unimplemented 2023-11-02 20:57:05 +00:00
unboxed-closures-zero-args.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00