Skip to content

Commit 9409bb8

Browse files
Refine deref suggestions
1 parent 2bddfca commit 9409bb8

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
800800
let mut is_raw_ptr = false;
801801
let mut is_ref = false;
802802
let mut is_destructuring_assignment = false;
803+
let mut is_nested_deref = false;
803804
if let Some(inner) = inner {
805+
is_nested_deref =
806+
matches!(inner.kind, hir::ExprKind::Unary(hir::UnOp::Deref, _));
804807
let typck_result = self.infcx.tcx.typeck(self.mir_def_id());
805808
if let Some(inner_type) = typck_result.node_type_opt(inner.hir_id) {
806809
if matches!(inner_type.kind(), ty::RawPtr(..)) {
@@ -822,21 +825,25 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
822825
}
823826
// If the `inner` is a raw pointer, do not suggest removing the "*", see #126863
824827
// FIXME: need to check whether the assigned object can be a raw pointer, see `tests/ui/borrowck/issue-20801.rs`.
825-
if !is_raw_ptr && (!is_destructuring_pattern_move || is_ref) {
828+
if is_raw_ptr {
829+
return;
830+
}
831+
832+
if !is_destructuring_pattern_move || is_ref {
826833
err.span_suggestion_verbose(
827834
span.with_hi(span.lo() + BytePos(1)),
828835
"consider removing the dereference here",
829836
String::new(),
830837
Applicability::MaybeIncorrect,
831838
);
832-
} else if !is_raw_ptr && !is_destructuring_assignment {
839+
} else if !is_destructuring_assignment && !is_nested_deref {
833840
err.span_suggestion_verbose(
834841
span.shrink_to_lo(),
835842
"consider borrowing here",
836843
'&',
837844
Applicability::MaybeIncorrect,
838845
);
839-
} else if !is_raw_ptr {
846+
} else {
840847
err.span_help(
841848
span,
842849
"destructuring assignment cannot borrow from this expression; consider using a `let` binding instead",

tests/ui/suggestions/issue-102892.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ LL | let (a, b) = **arc; // suggests putting `&**arc` here; with that, fixed
6161
| | ...and here
6262
| data moved here
6363
|
64-
= note: move occurs because these variables have types that don't implement the `Copy` trait
65-
help: consider removing the dereference here
66-
|
67-
LL - let (a, b) = **arc; // suggests putting `&**arc` here; with that, fixed!
68-
LL + let (a, b) = *arc; // suggests putting `&**arc` here; with that, fixed!
64+
help: destructuring assignment cannot borrow from this expression; consider using a `let` binding instead
65+
--> $DIR/issue-102892.rs:11:18
6966
|
67+
LL | let (a, b) = **arc; // suggests putting `&**arc` here; with that, fixed!
68+
| ^^^^^
69+
= note: move occurs because these variables have types that don't implement the `Copy` trait
7070

7171
error: aborting due to 4 previous errors
7272

0 commit comments

Comments
 (0)