From 4a51f37bcb58b37afb8ea0ac4b8696714787a305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 3 Nov 2022 09:19:03 -0700 Subject: [PATCH] Do not suggest `ref` multiple times for the same binding --- .../rustc_borrowck/src/diagnostics/conflict_errors.rs | 4 +++- ...fter-at-or-patterns-slice-patterns-box-patterns.stderr | 8 -------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 9b6836039a13..8a4b0ebe737e 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -171,6 +171,7 @@ pub(crate) fn report_use_of_moved_or_uninitialized( let mut is_loop_move = false; let mut in_pattern = false; + let mut seen_spans = FxHashSet::default(); for move_site in &move_site_vec { let move_out = self.move_data.moves[(*move_site).moi]; @@ -320,7 +321,7 @@ fn visit_pat(&mut self, p: &'hir hir::Pat<'hir>) { self.suggest_cloning(&mut err, ty, move_span); } } - if let Some(pat) = finder.pat { + if let Some(pat) = finder.pat && !seen_spans.contains(&pat.span) { in_pattern = true; err.span_suggestion_verbose( pat.span.shrink_to_lo(), @@ -328,6 +329,7 @@ fn visit_pat(&mut self, p: &'hir hir::Pat<'hir>) { "ref ".to_string(), Applicability::MachineApplicable, ); + seen_spans.insert(pat.span); } } diff --git a/src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr b/src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr index f58672f0666b..d06b3bc1c3e6 100644 --- a/src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr +++ b/src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr @@ -88,10 +88,6 @@ help: borrow this binding in the pattern to avoid moving the value | LL | ref foo @ Some(Test::Foo | Test::Bar) => (), | +++ -help: borrow this binding in the pattern to avoid moving the value - | -LL | ref foo @ Some(Test::Foo | Test::Bar) => (), - | +++ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable --> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:86:5 @@ -148,10 +144,6 @@ help: borrow this binding in the pattern to avoid moving the value | LL | ref a @ [.., Some(Test::Foo | Test::Bar)] => (), | +++ -help: borrow this binding in the pattern to avoid moving the value - | -LL | ref a @ [.., Some(Test::Foo | Test::Bar)] => (), - | +++ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable --> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:144:5