Skip to content

Commit

Permalink
Auto merge of #45947 - estebank:match_default_bindings-arg-hint, r=ar…
Browse files Browse the repository at this point in the history
…ielb1

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.
  • Loading branch information
bors committed Nov 26, 2017
2 parents 128b40f + 15dfd7e commit 693bb0d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,33 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
.pat_adjustments_mut()
.insert(pat.hir_id, pat_adjustments);
} else {
let mut ref_sp = pat.span;
let mut id = pat.id;
loop { // make span include all enclosing `&` to avoid confusing diag output
id = tcx.hir.get_parent_node(id);
let node = tcx.hir.find(id);
if let Some(hir::map::NodePat(pat)) = node {
if let hir::PatKind::Ref(..) = pat.node {
ref_sp = pat.span;
} else {
break;
}
} else {
break;
}
}
let sp = ref_sp.to(pat.span);
let mut err = feature_gate::feature_err(
&tcx.sess.parse_sess,
"match_default_bindings",
pat.span,
sp,
feature_gate::GateIssue::Language,
"non-reference pattern used to match a reference",
);
if let Ok(snippet) = tcx.sess.codemap().span_to_snippet(pat.span) {
err.span_suggestion(pat.span, "consider using", format!("&{}", &snippet));
if let Ok(snippet) = tcx.sess.codemap().span_to_snippet(sp) {
err.span_suggestion(sp,
"consider using a reference",
format!("&{}", &snippet));
}
err.emit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: non-reference pattern used to match a reference (see issue #42640)
--> $DIR/suggestion.rs:12:12
|
12 | if let Some(y) = &Some(22) { //~ ERROR non-reference pattern
| ^^^^^^^ help: consider using: `&Some(y)`
| ^^^^^^^ help: consider using a reference: `&Some(y)`
|
= help: add #![feature(match_default_bindings)] to the crate attributes to enable

Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/suggestions/dont-suggest-dereference-on-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn foo(s: &str) -> bool { true }

fn main() {
let x = vec![(String::new(), String::new())];
x.iter()
.filter(|&(ref a, _)| foo(a))
//~^ ERROR non-reference pattern used to match a reference
//~| HELP consider using a reference
//~| HELP add
.collect();
}
10 changes: 10 additions & 0 deletions src/test/ui/suggestions/dont-suggest-dereference-on-arg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: non-reference pattern used to match a reference (see issue #42640)
--> $DIR/dont-suggest-dereference-on-arg.rs:16:18
|
16 | .filter(|&(ref a, _)| foo(a))
| ^^^^^^^^^^^ help: consider using a reference: `&&(ref a, _)`
|
= help: add #![feature(match_default_bindings)] to the crate attributes to enable

error: aborting due to previous error

0 comments on commit 693bb0d

Please sign in to comment.