Skip to content

Commit 59d7d4c

Browse files
committed
only emit suggestion to move & if the inner pattern is a binding
1 parent 563ecc1 commit 59d7d4c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3030
self.check_pat_arg(pat, expected, false);
3131
}
3232

33+
/// The `is_arg` argument indicates whether this pattern is the
34+
/// *outermost* pattern in an argument (e.g., in `fn foo(&x:
35+
/// &u32)`, it is true for the `&x` pattern but not `x`). This is
36+
/// used to tailor error reporting.
3337
pub fn check_pat_arg(&self, pat: &'gcx hir::Pat, expected: Ty<'tcx>, is_arg: bool) {
3438
let tcx = self.tcx;
3539

36-
debug!("check_pat(pat={:?},expected={:?})", pat, expected);
40+
debug!("check_pat(pat={:?},expected={:?},is_arg={})", pat, expected, is_arg);
3741

3842
let ty = match pat.node {
3943
PatKind::Wild => {
@@ -206,6 +210,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
206210
// can, to avoid creating needless variables. This
207211
// also helps with the bad interactions of the given
208212
// hack detailed in (*) below.
213+
debug!("check_pat_arg: expected={:?}", expected);
209214
let (rptr_ty, inner_ty) = match expected.sty {
210215
ty::TyRef(_, mt) if mt.mutbl == mutbl => {
211216
(expected, mt.ty)
@@ -216,15 +221,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
216221
let mt = ty::TypeAndMut { ty: inner_ty, mutbl: mutbl };
217222
let region = self.next_region_var(infer::PatternRegion(pat.span));
218223
let rptr_ty = tcx.mk_ref(region, mt);
224+
debug!("check_pat_arg: demanding {:?} = {:?}", expected, rptr_ty);
219225
let err = self.demand_eqtype_diag(pat.span, expected, rptr_ty);
226+
227+
// Look for a case like `fn foo(&foo: u32)` and suggest
228+
// `fn foo(foo: &u32)`
220229
if let Some(mut err) = err {
221230
if is_arg {
222-
if let Ok(snippet) = self.sess().codemap()
223-
.span_to_snippet(pat.span)
224-
{
225-
err.help(&format!("did you mean `{}: &{}`?",
226-
&snippet[1..],
227-
expected));
231+
if let PatKind::Binding(..) = inner.node {
232+
if let Ok(snippet) = self.sess().codemap()
233+
.span_to_snippet(pat.span)
234+
{
235+
err.help(&format!("did you mean `{}: &{}`?",
236+
&snippet[1..],
237+
expected));
238+
}
228239
}
229240
}
230241
err.emit();

0 commit comments

Comments
 (0)