mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
![]() borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap` This PR aims to fix #132806 by rewriting `add_move_error_suggestions`[^1]. Previously, it manually scanned the source text to find a leading `&`, which isn't always going to produce a correct result (see: that issue). Admittedly, the HIR visitor in this PR introduces a lot of boilerplate, but hopefully the logic at its core isn't too complicated (I go over it in the comments). I also tried a simpler version that didn't use a HIR visitor and suggested adding `ref` always, but the `&ref x` suggestions really didn't look good. As a bonus for the added complexity though, it's now able to produce nice `&`-removing suggestions in more cases. I tried to do this such that it avoids edition-dependent checks and its suggestions can be applied together with those from the match ergonomics 2024 migration lint. I haven't added tests for that since the details of match ergonomics 2024 are still being sorted out, but I can try if desired once that's finalized. [^1]: In brief, it fires on patterns where users try to bind by-value in such a way that moves out of a reference to a non-Copy type (including slice references with non-copy elements). The suggestions are to change the binding's mode to be by-reference, either by removing[^2] an enclosing `&`/`&mut` or adding `ref` to the binding. [^2]: Incidentally, I find the terminology of "consider removing the borrow" a bit confusing for a suggestion to remove a `&` pattern in order to make bindings borrow rather than move. I'm not sure what a good, concise way to explain that would be though, and that should go in a separate PR anyway. |
||
---|---|---|
.. | ||
auxiliary | ||
assignment-of-clone-call-on-ref-due-to-missing-bound.fixed | ||
assignment-of-clone-call-on-ref-due-to-missing-bound.rs | ||
assignment-of-clone-call-on-ref-due-to-missing-bound.stderr | ||
borrow-closures-instead-of-move.rs | ||
borrow-closures-instead-of-move.stderr | ||
do-not-suggest-removing-wrong-ref-pattern-issue-132806.fixed | ||
do-not-suggest-removing-wrong-ref-pattern-issue-132806.rs | ||
do-not-suggest-removing-wrong-ref-pattern-issue-132806.stderr | ||
issue-22536-copy-mustnt-zero.rs | ||
issue-22536-copy-mustnt-zero.stderr | ||
issue-34721.fixed | ||
issue-34721.rs | ||
issue-34721.stderr | ||
issue-46099-move-in-macro.rs | ||
issue-46099-move-in-macro.stderr | ||
issue-72649-uninit-in-loop.rs | ||
issue-72649-uninit-in-loop.stderr | ||
issue-75904-move-closure-loop.rs | ||
issue-75904-move-closure-loop.stderr | ||
issue-99470-move-out-of-some.rs | ||
issue-99470-move-out-of-some.stderr | ||
move-1-unique.rs | ||
move-2-unique.rs | ||
move-2.rs | ||
move-3-unique.rs | ||
move-4-unique.rs | ||
move-4.rs | ||
move-arg-2-unique.rs | ||
move-arg-2.rs | ||
move-arg.rs | ||
move-deref-coercion.rs | ||
move-deref-coercion.stderr | ||
move-fn-self-receiver.rs | ||
move-fn-self-receiver.stderr | ||
move-guard-same-consts.rs | ||
move-guard-same-consts.stderr | ||
move-in-guard-1.rs | ||
move-in-guard-1.stderr | ||
move-in-guard-2.rs | ||
move-in-guard-2.stderr | ||
move-into-dead-array-1.rs | ||
move-into-dead-array-1.stderr | ||
move-into-dead-array-2.rs | ||
move-into-dead-array-2.stderr | ||
move-nullary-fn.rs | ||
move-of-addr-of-mut.rs | ||
move-of-addr-of-mut.stderr | ||
move-out-of-array-1.rs | ||
move-out-of-array-1.stderr | ||
move-out-of-array-ref.rs | ||
move-out-of-array-ref.stderr | ||
move-out-of-field.rs | ||
move-out-of-slice-1.rs | ||
move-out-of-slice-1.stderr | ||
move-out-of-slice-2.rs | ||
move-out-of-slice-2.stderr | ||
move-out-of-tuple-field.rs | ||
move-out-of-tuple-field.stderr | ||
move-scalar.rs | ||
moved-value-on-as-ref-arg.fixed | ||
moved-value-on-as-ref-arg.rs | ||
moved-value-on-as-ref-arg.stderr | ||
moves-based-on-type-access-to-field.rs | ||
moves-based-on-type-access-to-field.stderr | ||
moves-based-on-type-block-bad.rs | ||
moves-based-on-type-block-bad.stderr | ||
moves-based-on-type-capture-clause-bad.rs | ||
moves-based-on-type-capture-clause-bad.stderr | ||
moves-based-on-type-capture-clause.rs | ||
moves-based-on-type-cyclic-types-issue-4821.rs | ||
moves-based-on-type-cyclic-types-issue-4821.stderr | ||
moves-based-on-type-distribute-copy-over-paren.rs | ||
moves-based-on-type-distribute-copy-over-paren.stderr | ||
moves-based-on-type-exprs.rs | ||
moves-based-on-type-exprs.stderr | ||
moves-based-on-type-match-bindings.rs | ||
moves-based-on-type-match-bindings.stderr | ||
moves-based-on-type-move-out-of-closure-env-issue-1965.rs | ||
moves-based-on-type-move-out-of-closure-env-issue-1965.stderr | ||
moves-based-on-type-no-recursive-stack-closure.rs | ||
moves-based-on-type-no-recursive-stack-closure.stderr | ||
moves-based-on-type-tuple.rs | ||
moves-based-on-type-tuple.stderr | ||
moves-sru-moved-field.rs | ||
moves-sru-moved-field.stderr | ||
needs-clone-through-deref.fixed | ||
needs-clone-through-deref.rs | ||
needs-clone-through-deref.stderr | ||
nested-loop-moved-value-wrong-continue.rs | ||
nested-loop-moved-value-wrong-continue.stderr | ||
pin-mut-reborrow-infer-var-issue-107419.fixed | ||
pin-mut-reborrow-infer-var-issue-107419.rs | ||
pin-mut-reborrow-infer-var-issue-107419.stderr | ||
pin-mut-reborrow.fixed | ||
pin-mut-reborrow.rs | ||
pin-mut-reborrow.stderr | ||
recreating-value-in-loop-condition.rs | ||
recreating-value-in-loop-condition.stderr | ||
region-var-in-moved-ty-issue-133118.rs | ||
region-var-in-moved-ty-issue-133118.stderr | ||
suggest-borrow-for-generic-arg.fixed | ||
suggest-borrow-for-generic-arg.rs | ||
suggest-borrow-for-generic-arg.stderr | ||
suggest-clone-when-some-obligation-is-unmet.fixed | ||
suggest-clone-when-some-obligation-is-unmet.rs | ||
suggest-clone-when-some-obligation-is-unmet.stderr | ||
suggest-clone.fixed | ||
suggest-clone.rs | ||
suggest-clone.stderr | ||
use_of_moved_value_clone_suggestions.rs | ||
use_of_moved_value_clone_suggestions.stderr | ||
use_of_moved_value_copy_suggestions.fixed | ||
use_of_moved_value_copy_suggestions.rs | ||
use_of_moved_value_copy_suggestions.stderr |