Skip to content

Commit 2d15732

Browse files
committed
Auto merge of #95127 - notriddle:notriddle/option-content-move-from-tuple-match, r=estebank
diagnostics: do not give Option::as_ref suggestion for complex match Fixes #82528
2 parents cf85310 + 306dcd6 commit 2d15732

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,29 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
218218

219219
fn report(&mut self, error: GroupedMoveError<'tcx>) {
220220
let (mut err, err_span) = {
221-
let (span, use_spans, original_path, kind): (
221+
let (span, use_spans, original_path, kind, has_complex_bindings): (
222222
Span,
223223
Option<UseSpans<'tcx>>,
224224
Place<'tcx>,
225225
&IllegalMoveOriginKind<'_>,
226+
bool,
226227
) = match error {
227-
GroupedMoveError::MovesFromPlace { span, original_path, ref kind, .. }
228-
| GroupedMoveError::MovesFromValue { span, original_path, ref kind, .. } => {
229-
(span, None, original_path, kind)
228+
GroupedMoveError::MovesFromPlace {
229+
span,
230+
original_path,
231+
ref kind,
232+
ref binds_to,
233+
..
230234
}
235+
| GroupedMoveError::MovesFromValue {
236+
span,
237+
original_path,
238+
ref kind,
239+
ref binds_to,
240+
..
241+
} => (span, None, original_path, kind, !binds_to.is_empty()),
231242
GroupedMoveError::OtherIllegalMove { use_spans, original_path, ref kind } => {
232-
(use_spans.args_or_use(), Some(use_spans), original_path, kind)
243+
(use_spans.args_or_use(), Some(use_spans), original_path, kind, false)
233244
}
234245
};
235246
debug!(
@@ -248,6 +259,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
248259
target_place,
249260
span,
250261
use_spans,
262+
has_complex_bindings,
251263
),
252264
&IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
253265
self.cannot_move_out_of_interior_of_drop(span, ty)
@@ -290,6 +302,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
290302
deref_target_place: Place<'tcx>,
291303
span: Span,
292304
use_spans: Option<UseSpans<'tcx>>,
305+
has_complex_bindings: bool,
293306
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
294307
// Inspect the type of the content behind the
295308
// borrow to provide feedback about why this
@@ -399,6 +412,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
399412
let diag_name = self.infcx.tcx.get_diagnostic_name(def_id);
400413
if matches!(diag_name, Some(sym::Option | sym::Result))
401414
&& use_spans.map_or(true, |v| !v.for_closure())
415+
&& !has_complex_bindings
402416
{
403417
err.span_suggestion_verbose(
404418
span.shrink_to_hi(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo(a: &Option<String>, b: &Option<String>) {
2+
match (a, b) {
3+
//~^ ERROR cannot move out of a shared reference
4+
(None, &c) => &c.unwrap(),
5+
(&Some(ref c), _) => c,
6+
};
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0507]: cannot move out of a shared reference
2+
--> $DIR/option-content-move-from-tuple-match.rs:2:11
3+
|
4+
LL | match (a, b) {
5+
| ^^^^^^
6+
LL |
7+
LL | (None, &c) => &c.unwrap(),
8+
| -
9+
| |
10+
| data moved here
11+
| move occurs because `c` has type `Option<String>`, which does not implement the `Copy` trait
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)