rust/compiler/rustc_hir/src
Esteban Küber 14473adf42 Detect when move of !Copy value occurs within loop and should likely not be cloned
When encountering a move error on a value within a loop of any kind,
identify if the moved value belongs to a call expression that should not
be cloned and avoid the semantically incorrect suggestion. Also try to
suggest moving the call expression outside of the loop instead.

```
error[E0382]: use of moved value: `vec`
  --> $DIR/recreating-value-in-loop-condition.rs:6:33
   |
LL |     let vec = vec!["one", "two", "three"];
   |         --- move occurs because `vec` has type `Vec<&str>`, which does not implement the `Copy` trait
LL |     while let Some(item) = iter(vec).next() {
   |     ----------------------------^^^--------
   |     |                           |
   |     |                           value moved here, in previous iteration of loop
   |     inside of this loop
   |
note: consider changing this parameter type in function `iter` to borrow instead if owning the value isn't necessary
  --> $DIR/recreating-value-in-loop-condition.rs:1:17
   |
LL | fn iter<T>(vec: Vec<T>) -> impl Iterator<Item = T> {
   |    ----         ^^^^^^ this parameter takes ownership of the value
   |    |
   |    in this function
help: consider moving the expression out of the loop so it is only moved once
   |
LL ~     let mut value = iter(vec);
LL ~     while let Some(item) = value.next() {
   |
```

We use the presence of a `break` in the loop that would be affected by
the moved value as a heuristic for "shouldn't be cloned".

Fix #121466.
2024-03-17 21:32:26 +00:00
..
arena.rs Remove unused arena kinds in rustc_hir and rustc_middle. 2023-11-20 15:05:26 +11:00
def_path_hash_map.rs Do not store stable crate id in on-disk hash map. 2023-12-24 17:22:48 +00:00
def.rs Some comment nits 2024-03-12 08:51:20 +00:00
definitions.rs rename RPITIT from opaque to synthetic 2024-02-27 17:43:40 +00:00
diagnostic_items.rs Replace some instances of FxHashMap/FxHashSet with stable alternatives (mostly in rustc_hir and rustc_ast_lowering) 2023-12-18 21:03:32 +01:00
hir_id.rs Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
hir.rs Detect when move of !Copy value occurs within loop and should likely not be cloned 2024-03-17 21:32:26 +00:00
intravisit.rs Rename hir::StmtKind::Local into hir::StmtKind::Let 2024-03-14 12:42:04 +01:00
lang_items.rs only set noalias on Box with the global allocator 2024-03-05 15:03:33 +01:00
lib.rs Remove unnecessary min_specialization after bootstrap 2024-02-10 18:15:11 +11:00
pat_util.rs Fix liveness analysis in the presence of never patterns 2024-02-21 12:04:39 +01:00
stable_hash_impls.rs Simplify BodyId hashing. 2024-01-16 23:52:30 +00:00
target.rs Change DefKind::Static to a struct variant 2024-03-12 05:53:46 +00:00
tests.rs Replace two create_default_session_if_not_set_then uses. 2023-11-02 19:35:04 +11:00
weak_lang_items.rs Rewrite implementation of #[alloc_error_handler] 2022-10-31 16:32:57 +00:00