rust/compiler
bors 5608c7f9aa Auto merge of #121652 - estebank:move-in-loop-break-condition, r=Nadrieril
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 https://github.com/rust-lang/rust/issues/121466.

---

*Point at continue and break that might be in the wrong place*

Sometimes move errors are because of a misplaced `continue`, but we didn't
surface that anywhere. Now when there are more than one set of nested loops
we show them out and point at the `continue` and `break` expressions within
that might need to go elsewhere.

```
error[E0382]: use of moved value: `foo`
  --> $DIR/nested-loop-moved-value-wrong-continue.rs:46:18
   |
LL |     for foo in foos {
   |         ---
   |         |
   |         this reinitialization might get skipped
   |         move occurs because `foo` has type `String`, which does not implement the `Copy` trait
...
LL |         for bar in &bars {
   |         ---------------- inside of this loop
...
LL |                 baz.push(foo);
   |                          --- value moved here, in previous iteration of loop
...
LL |         qux.push(foo);
   |                  ^^^ value used here after move
   |
note: verify that your loop breaking logic is correct
  --> $DIR/nested-loop-moved-value-wrong-continue.rs:41:17
   |
LL |     for foo in foos {
   |     ---------------
...
LL |         for bar in &bars {
   |         ----------------
...
LL |                 continue;
   |                 ^^^^^^^^ this `continue` advances the loop at line 33
help: consider moving the expression out of the loop so it is only moved once
   |
LL ~         let mut value = baz.push(foo);
LL ~         for bar in &bars {
LL |
 ...
LL |             if foo == *bar {
LL ~                 value;
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL |                 baz.push(foo.clone());
   |                             ++++++++
```

Fix https://github.com/rust-lang/rust/issues/92531.
2024-03-18 02:10:34 +00:00
..
rustc Clean up rustc_*/Cargo.toml. 2023-10-30 08:46:02 +11:00
rustc_abi Auto merge of #121668 - erikdesjardins:commonprim, r=scottmcm,oli-obk 2024-03-13 15:25:35 +00:00
rustc_arena rename ptr::invalid -> ptr::without_provenance 2024-02-21 20:15:52 +01:00
rustc_ast Auto merge of #121926 - tgross35:f16-f128-step3-feature-gate, r=compiler-errors,petrochenkov 2024-03-16 02:02:00 +00:00
rustc_ast_ir Fix typo in VisitorResult 2024-03-08 23:20:29 -05:00
rustc_ast_lowering Rollup merge of #122564 - Bryanskiy:delegation-fixes, r=compiler-errors 2024-03-16 23:28:48 +01:00
rustc_ast_passes Rollup merge of #121545 - gvozdvmozgu:fix-attribute-validation-associated-items, r=fmease 2024-03-16 23:28:47 +01:00
rustc_ast_pretty Rename ast::StmtKind::Local into ast::StmtKind::Let 2024-03-14 12:42:04 +01:00
rustc_attr Rename IntoDiagnostic as Diagnostic. 2024-03-11 09:15:09 +11:00
rustc_baked_icu_data Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_borrowck Move suggest_hoisting_call_outside_loop out of suggest_cloning 2024-03-17 21:52:12 +00:00
rustc_builtin_macros delay expand macro bang when there has indeterminate path 2024-03-13 16:11:16 +08:00
rustc_codegen_cranelift Merge commit '4cf4ffc6ba' into sync_cg_clif-2024-03-16 2024-03-16 17:23:11 +00:00
rustc_codegen_gcc Some comment nits 2024-03-12 08:51:20 +00:00
rustc_codegen_llvm Register LLVM handlers for bad-alloc / OOM 2024-03-15 15:49:06 -07:00
rustc_codegen_ssa Auto merge of #121297 - michaelwoerister:set-pdb-alt-path, r=wesleywiser 2024-03-15 14:14:34 +00:00
rustc_const_eval Rename some things around validation error reporting to signal that it is in fact about validation failures 2024-03-14 12:21:35 +00:00
rustc_data_structures Issue 122262: MAP_PRIVATE for more reliability on virtualised filesystems. 2024-03-15 18:31:07 -04:00
rustc_driver Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_driver_impl Rollup merge of #121764 - Zoxc:incr-sess-no-source, r=oli-obk 2024-03-14 20:00:18 +01:00
rustc_error_codes Improve error message for opaque captures 2024-03-08 19:08:13 +00:00
rustc_error_messages Rename SubdiagnosticMessage as SubdiagMessage. 2024-03-05 12:14:49 +11:00
rustc_errors Make SubdiagMessageOp well-formed 2024-03-14 13:13:08 -04:00
rustc_expand Rename ast::StmtKind::Local into ast::StmtKind::Let 2024-03-14 12:42:04 +01:00
rustc_feature Auto merge of #121926 - tgross35:f16-f128-step3-feature-gate, r=compiler-errors,petrochenkov 2024-03-16 02:02:00 +00:00
rustc_fluent_macro Rename SubdiagnosticMessage as SubdiagMessage. 2024-03-05 12:14:49 +11:00
rustc_fs_util Invert diagnostic lints. 2024-02-06 13:12:33 +11:00
rustc_graphviz Invert diagnostic lints. 2024-02-06 13:12:33 +11:00
rustc_hir Detect when move of !Copy value occurs within loop and should likely not be cloned 2024-03-17 21:32:26 +00:00
rustc_hir_analysis avoid unnecessary collect() 2024-03-17 12:19:46 +01:00
rustc_hir_pretty Rollup merge of #122487 - GuillaumeGomez:rename-stmtkind-local, r=oli-obk 2024-03-14 20:00:21 +01:00
rustc_hir_typeck Rollup merge of #122636 - matthiaskrgr:compl3, r=compiler-errors 2024-03-17 19:26:23 +01:00
rustc_incremental Make incremental sessions identity no longer depend on the crate names provided by source code 2024-03-13 16:40:02 +01:00
rustc_index doc: Add better explanation 2024-03-06 16:54:42 +01:00
rustc_index_macros Step all bootstrap cfgs forward 2024-02-08 07:44:34 -05:00
rustc_infer Rollup merge of #122513 - petrochenkov:somehir4, r=fmease 2024-03-15 17:24:09 +01:00
rustc_interface Rollup merge of #121764 - Zoxc:incr-sess-no-source, r=oli-obk 2024-03-14 20:00:18 +01:00
rustc_lexer Invert diagnostic lints. 2024-02-06 13:12:33 +11:00
rustc_lint Rollup merge of #122608 - Urgau:check-cfg-move-diagnostic-logic, r=fmease 2024-03-17 08:23:26 +01:00
rustc_lint_defs Rollup merge of #121720 - tmandry:split-refining, r=compiler-errors 2024-03-16 23:28:47 +01:00
rustc_llvm Install the bad-alloc handler before fatal errors 2024-03-15 16:49:08 -07:00
rustc_log rustc_log: expose tracing-tree "wraparound" in an env var 2024-03-03 12:33:26 +01:00
rustc_macros Rename diagnostic derive things. 2024-03-11 10:06:34 +11:00
rustc_metadata Rollup merge of #122605 - osiewicz:metadata-register-crate-store-crate-name-in-profile, r=Nadrieril 2024-03-16 23:28:50 +01:00
rustc_middle Guard decorate on when not to skip instead 2024-03-17 15:07:22 +00:00
rustc_mir_build Auto merge of #121926 - tgross35:f16-f128-step3-feature-gate, r=compiler-errors,petrochenkov 2024-03-16 02:02:00 +00:00
rustc_mir_dataflow Distinguish between library and lang UB in assert_unsafe_precondition 2024-03-08 18:53:58 -05:00
rustc_mir_transform some minor code simplifications 2024-03-17 13:44:44 +01:00
rustc_monomorphize collector: move ensure_sufficient_stack out of the loop 2024-03-17 15:17:00 +01:00
rustc_next_trait_solver Allow a way to add constructors for rustc_type_ir types 2024-03-04 15:39:59 +00:00
rustc_parse Rename ast::StmtKind::Local into ast::StmtKind::Let 2024-03-14 12:42:04 +01:00
rustc_parse_format remove a couple of redundant clones 2024-02-17 12:46:18 +01:00
rustc_passes Auto merge of #122371 - oli-obk:visit_nested_body, r=tmiasko 2024-03-16 04:35:02 +00:00
rustc_pattern_analysis Rollup merge of #122437 - Nadrieril:no-after-max, r=compiler-errors 2024-03-13 20:01:57 +01:00
rustc_privacy Test and implement reachability for trait objects and generic parameters of functions 2024-03-14 14:10:45 +00:00
rustc_query_impl Verify that query keys result in unique dep nodes 2024-03-12 05:31:41 +01:00
rustc_query_system Rollup merge of #122245 - saethlin:check-dep-graph-size, r=petrochenkov 2024-03-12 09:04:00 -07:00
rustc_resolve Auto merge of #121926 - tgross35:f16-f128-step3-feature-gate, r=compiler-errors,petrochenkov 2024-03-16 02:02:00 +00:00
rustc_serialize Replace NonZero::<_>::new with NonZero::new. 2024-02-15 08:09:42 +01:00
rustc_session some minor code simplifications 2024-03-17 13:44:44 +01:00
rustc_smir preserve span when evaluating mir::ConstOperand 2024-03-14 21:55:07 +01:00
rustc_span Add rustc_never_type_mode = "no_fallback" 2024-03-15 17:48:26 +00:00
rustc_symbol_mangling Rename IntoDiagnostic as Diagnostic. 2024-03-11 09:15:09 +11:00
rustc_target Rollup merge of #122212 - erikdesjardins:byval-align2, r=wesleywiser 2024-03-14 20:00:18 +01:00
rustc_trait_selection Auto merge of #122571 - matthiaskrgr:rollup-36wwovk, r=matthiaskrgr 2024-03-15 21:18:36 +00:00
rustc_traits Merge check_mod_impl_wf and check_mod_type_wf 2024-03-07 06:27:09 +00:00
rustc_transmute Safe Transmute: Use 'not yet supported', not 'unspecified' in errors 2024-03-15 17:42:29 +00:00
rustc_ty_utils Rollup merge of #122513 - petrochenkov:somehir4, r=fmease 2024-03-15 17:24:09 +01:00
rustc_type_ir Remove Ord from ClosureKind 2024-03-09 21:16:43 +08:00
stable_mir Rollup merge of #122405 - celinval:smir-new-const, r=oli-obk 2024-03-14 11:09:58 +01:00