Skip to content

Commit 693bb0d

Browse files
committed
Auto merge of #45947 - estebank:match_default_bindings-arg-hint, r=arielb1
Be more obvious when suggesting dereference Include `&` span when suggesting dereference on a span that is already a reference: ``` error: non-reference pattern used to match a reference (see issue #42640) --> dont-suggest-dereference-on-arg.rs:16:19 | 16 | .filter(|&(ref a, _)| foo(a)) | ^^^^^^^^^^^ help: consider using: `&&(ref k, _)` | = help: add #![feature(match_default_bindings)] to the crate attributes to enable ``` Fix #45925.
2 parents 128b40f + 15dfd7e commit 693bb0d

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

src/librustc_typeck/check/_match.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,33 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
120120
.pat_adjustments_mut()
121121
.insert(pat.hir_id, pat_adjustments);
122122
} else {
123+
let mut ref_sp = pat.span;
124+
let mut id = pat.id;
125+
loop { // make span include all enclosing `&` to avoid confusing diag output
126+
id = tcx.hir.get_parent_node(id);
127+
let node = tcx.hir.find(id);
128+
if let Some(hir::map::NodePat(pat)) = node {
129+
if let hir::PatKind::Ref(..) = pat.node {
130+
ref_sp = pat.span;
131+
} else {
132+
break;
133+
}
134+
} else {
135+
break;
136+
}
137+
}
138+
let sp = ref_sp.to(pat.span);
123139
let mut err = feature_gate::feature_err(
124140
&tcx.sess.parse_sess,
125141
"match_default_bindings",
126-
pat.span,
142+
sp,
127143
feature_gate::GateIssue::Language,
128144
"non-reference pattern used to match a reference",
129145
);
130-
if let Ok(snippet) = tcx.sess.codemap().span_to_snippet(pat.span) {
131-
err.span_suggestion(pat.span, "consider using", format!("&{}", &snippet));
146+
if let Ok(snippet) = tcx.sess.codemap().span_to_snippet(sp) {
147+
err.span_suggestion(sp,
148+
"consider using a reference",
149+
format!("&{}", &snippet));
132150
}
133151
err.emit();
134152
}

src/test/ui/rfc-2005-default-binding-mode/suggestion.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: non-reference pattern used to match a reference (see issue #42640)
22
--> $DIR/suggestion.rs:12:12
33
|
44
12 | if let Some(y) = &Some(22) { //~ ERROR non-reference pattern
5-
| ^^^^^^^ help: consider using: `&Some(y)`
5+
| ^^^^^^^ help: consider using a reference: `&Some(y)`
66
|
77
= help: add #![feature(match_default_bindings)] to the crate attributes to enable
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo(s: &str) -> bool { true }
12+
13+
fn main() {
14+
let x = vec![(String::new(), String::new())];
15+
x.iter()
16+
.filter(|&(ref a, _)| foo(a))
17+
//~^ ERROR non-reference pattern used to match a reference
18+
//~| HELP consider using a reference
19+
//~| HELP add
20+
.collect();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: non-reference pattern used to match a reference (see issue #42640)
2+
--> $DIR/dont-suggest-dereference-on-arg.rs:16:18
3+
|
4+
16 | .filter(|&(ref a, _)| foo(a))
5+
| ^^^^^^^^^^^ help: consider using a reference: `&&(ref a, _)`
6+
|
7+
= help: add #![feature(match_default_bindings)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)