Skip to content

Commit 693eea5

Browse files
committed
review comments
1 parent ff68673 commit 693eea5

File tree

4 files changed

+60
-64
lines changed

4 files changed

+60
-64
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::check::{FnCtxt, Expectation, Diverges, Needs};
22
use crate::check::coercion::CoerceMany;
33
use crate::util::nodemap::FxHashMap;
4-
use errors::Applicability;
5-
use rustc::hir::{self, PatKind};
4+
use errors::{Applicability, DiagnosticBuilder};
5+
use rustc::hir::{self, PatKind, Pat};
66
use rustc::hir::def::{Def, CtorKind};
77
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
88
use rustc::infer;
@@ -377,64 +377,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
377377
// Look for a case like `fn foo(&foo: u32)` and suggest
378378
// `fn foo(foo: &u32)`
379379
if let Some(mut err) = err {
380-
if let PatKind::Binding(..) = inner.node {
381-
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
382-
let parent = tcx.hir().get_by_hir_id(parent_id);
383-
debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
384-
match parent {
385-
hir::Node::Item(hir::Item {
386-
node: hir::ItemKind::Fn(..), ..
387-
}) |
388-
hir::Node::ForeignItem(hir::ForeignItem {
389-
node: hir::ForeignItemKind::Fn(..), ..
390-
}) |
391-
hir::Node::TraitItem(hir::TraitItem {
392-
node: hir::TraitItemKind::Method(..), ..
393-
}) |
394-
hir::Node::ImplItem(hir::ImplItem {
395-
node: hir::ImplItemKind::Method(..), ..
396-
}) => { // this pat is likely an argument
397-
if let Ok(snippet) = tcx.sess.source_map()
398-
.span_to_snippet(inner.span)
399-
{ // FIXME: turn into structured suggestion, will need
400-
// a span that also includes the the arg's type.
401-
err.help(&format!(
402-
"did you mean `{}: &{}`?",
403-
snippet,
404-
expected,
405-
));
406-
}
407-
}
408-
hir::Node::Expr(hir::Expr {
409-
node: hir::ExprKind::Match(..), ..
410-
}) => { // rely on match ergonomics
411-
if let Ok(snippet) = tcx.sess.source_map()
412-
.span_to_snippet(inner.span)
413-
{
414-
err.span_suggestion(
415-
pat.span,
416-
"you can rely on match ergonomics and remove \
417-
the explicit borrow",
418-
snippet,
419-
Applicability::MaybeIncorrect,
420-
);
421-
}
422-
}
423-
hir::Node::Pat(_) => { // nested `&&pat`
424-
if let Ok(snippet) = tcx.sess.source_map()
425-
.span_to_snippet(inner.span)
426-
{
427-
err.span_suggestion(
428-
pat.span,
429-
"you can probaly remove the explicit borrow",
430-
snippet,
431-
Applicability::MaybeIncorrect,
432-
);
433-
}
434-
}
435-
_ => {} // don't provide suggestions in other cases #55175
436-
}
437-
}
380+
self.borrow_pat_suggestion(&mut err, &pat, &inner, &expected);
438381
err.emit();
439382
}
440383
(rptr_ty, inner_ty)
@@ -566,6 +509,59 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
566509
// subtyping.
567510
}
568511

512+
fn borrow_pat_suggestion(
513+
&self,
514+
err: &mut DiagnosticBuilder<'_>,
515+
pat: &Pat,
516+
inner: &Pat,
517+
expected: &Ty<'tcx>,
518+
) {
519+
let tcx = self.tcx;
520+
if let PatKind::Binding(..) = inner.node {
521+
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
522+
let parent = tcx.hir().get_by_hir_id(parent_id);
523+
debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
524+
match parent {
525+
hir::Node::Item(hir::Item { node: hir::ItemKind::Fn(..), .. }) |
526+
hir::Node::ForeignItem(hir::ForeignItem {
527+
node: hir::ForeignItemKind::Fn(..), ..
528+
}) |
529+
hir::Node::TraitItem(hir::TraitItem { node: hir::TraitItemKind::Method(..), .. }) |
530+
hir::Node::ImplItem(hir::ImplItem { node: hir::ImplItemKind::Method(..), .. }) => {
531+
// this pat is likely an argument
532+
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
533+
// FIXME: turn into structured suggestion, will need a span that also
534+
// includes the the arg's type.
535+
err.help(&format!("did you mean `{}: &{}`?", snippet, expected));
536+
}
537+
}
538+
hir::Node::Expr(hir::Expr { node: hir::ExprKind::Match(..), .. }) => {
539+
// rely on match ergonomics
540+
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
541+
err.span_suggestion(
542+
pat.span,
543+
"you can rely on match ergonomics and remove the explicit borrow",
544+
snippet,
545+
Applicability::MaybeIncorrect,
546+
);
547+
}
548+
}
549+
hir::Node::Pat(_) => {
550+
// nested `&&pat`
551+
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
552+
err.span_suggestion(
553+
pat.span,
554+
"you can probably remove the explicit borrow",
555+
snippet,
556+
Applicability::MaybeIncorrect,
557+
);
558+
}
559+
}
560+
_ => {} // don't provide suggestions in other cases #55175
561+
}
562+
}
563+
}
564+
569565
pub fn check_dereferencable(&self, span: Span, expected: Ty<'tcx>, inner: &hir::Pat) -> bool {
570566
if let PatKind::Binding(..) = inner.node {
571567
if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true) {

src/test/ui/destructure-trait-ref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | let &&x = &1isize as &T;
2323
| ^^
2424
| |
2525
| expected trait T, found reference
26-
| help: you can probaly remove the explicit borrow: `x`
26+
| help: you can probably remove the explicit borrow: `x`
2727
|
2828
= note: expected type `dyn T`
2929
found type `&_`
@@ -35,7 +35,7 @@ LL | let &&&x = &(&1isize as &T);
3535
| ^^
3636
| |
3737
| expected trait T, found reference
38-
| help: you can probaly remove the explicit borrow: `x`
38+
| help: you can probably remove the explicit borrow: `x`
3939
|
4040
= note: expected type `dyn T`
4141
found type `&_`

src/test/ui/mismatched_types/issue-38371.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | fn agh(&&bar: &u32) {
1515
| ^^^^
1616
| |
1717
| expected u32, found reference
18-
| help: you can probaly remove the explicit borrow: `bar`
18+
| help: you can probably remove the explicit borrow: `bar`
1919
|
2020
= note: expected type `u32`
2121
found type `&_`

src/test/ui/suggestions/match-ergonomics.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | [&v] => {},
55
| ^^
66
| |
77
| expected i32, found reference
8-
| help: you can probaly remove the explicit borrow: `v`
8+
| help: you can probably remove the explicit borrow: `v`
99
|
1010
= note: expected type `i32`
1111
found type `&_`

0 commit comments

Comments
 (0)