review comments
This commit is contained in:
parent
ff68673387
commit
693eea5784
4 changed files with 60 additions and 64 deletions
|
@ -1,8 +1,8 @@
|
|||
use crate::check::{FnCtxt, Expectation, Diverges, Needs};
|
||||
use crate::check::coercion::CoerceMany;
|
||||
use crate::util::nodemap::FxHashMap;
|
||||
use errors::Applicability;
|
||||
use rustc::hir::{self, PatKind};
|
||||
use errors::{Applicability, DiagnosticBuilder};
|
||||
use rustc::hir::{self, PatKind, Pat};
|
||||
use rustc::hir::def::{Def, CtorKind};
|
||||
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
|
||||
use rustc::infer;
|
||||
|
@ -377,64 +377,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
// Look for a case like `fn foo(&foo: u32)` and suggest
|
||||
// `fn foo(foo: &u32)`
|
||||
if let Some(mut err) = err {
|
||||
if let PatKind::Binding(..) = inner.node {
|
||||
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
|
||||
let parent = tcx.hir().get_by_hir_id(parent_id);
|
||||
debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
|
||||
match parent {
|
||||
hir::Node::Item(hir::Item {
|
||||
node: hir::ItemKind::Fn(..), ..
|
||||
}) |
|
||||
hir::Node::ForeignItem(hir::ForeignItem {
|
||||
node: hir::ForeignItemKind::Fn(..), ..
|
||||
}) |
|
||||
hir::Node::TraitItem(hir::TraitItem {
|
||||
node: hir::TraitItemKind::Method(..), ..
|
||||
}) |
|
||||
hir::Node::ImplItem(hir::ImplItem {
|
||||
node: hir::ImplItemKind::Method(..), ..
|
||||
}) => { // this pat is likely an argument
|
||||
if let Ok(snippet) = tcx.sess.source_map()
|
||||
.span_to_snippet(inner.span)
|
||||
{ // FIXME: turn into structured suggestion, will need
|
||||
// a span that also includes the the arg's type.
|
||||
err.help(&format!(
|
||||
"did you mean `{}: &{}`?",
|
||||
snippet,
|
||||
expected,
|
||||
));
|
||||
}
|
||||
}
|
||||
hir::Node::Expr(hir::Expr {
|
||||
node: hir::ExprKind::Match(..), ..
|
||||
}) => { // rely on match ergonomics
|
||||
if let Ok(snippet) = tcx.sess.source_map()
|
||||
.span_to_snippet(inner.span)
|
||||
{
|
||||
err.span_suggestion(
|
||||
pat.span,
|
||||
"you can rely on match ergonomics and remove \
|
||||
the explicit borrow",
|
||||
snippet,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
hir::Node::Pat(_) => { // nested `&&pat`
|
||||
if let Ok(snippet) = tcx.sess.source_map()
|
||||
.span_to_snippet(inner.span)
|
||||
{
|
||||
err.span_suggestion(
|
||||
pat.span,
|
||||
"you can probaly remove the explicit borrow",
|
||||
snippet,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {} // don't provide suggestions in other cases #55175
|
||||
}
|
||||
}
|
||||
self.borrow_pat_suggestion(&mut err, &pat, &inner, &expected);
|
||||
err.emit();
|
||||
}
|
||||
(rptr_ty, inner_ty)
|
||||
|
@ -566,6 +509,59 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
// subtyping.
|
||||
}
|
||||
|
||||
fn borrow_pat_suggestion(
|
||||
&self,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
pat: &Pat,
|
||||
inner: &Pat,
|
||||
expected: &Ty<'tcx>,
|
||||
) {
|
||||
let tcx = self.tcx;
|
||||
if let PatKind::Binding(..) = inner.node {
|
||||
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
|
||||
let parent = tcx.hir().get_by_hir_id(parent_id);
|
||||
debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
|
||||
match parent {
|
||||
hir::Node::Item(hir::Item { node: hir::ItemKind::Fn(..), .. }) |
|
||||
hir::Node::ForeignItem(hir::ForeignItem {
|
||||
node: hir::ForeignItemKind::Fn(..), ..
|
||||
}) |
|
||||
hir::Node::TraitItem(hir::TraitItem { node: hir::TraitItemKind::Method(..), .. }) |
|
||||
hir::Node::ImplItem(hir::ImplItem { node: hir::ImplItemKind::Method(..), .. }) => {
|
||||
// this pat is likely an argument
|
||||
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
|
||||
// FIXME: turn into structured suggestion, will need a span that also
|
||||
// includes the the arg's type.
|
||||
err.help(&format!("did you mean `{}: &{}`?", snippet, expected));
|
||||
}
|
||||
}
|
||||
hir::Node::Expr(hir::Expr { node: hir::ExprKind::Match(..), .. }) => {
|
||||
// rely on match ergonomics
|
||||
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
|
||||
err.span_suggestion(
|
||||
pat.span,
|
||||
"you can rely on match ergonomics and remove the explicit borrow",
|
||||
snippet,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
hir::Node::Pat(_) => {
|
||||
// nested `&&pat`
|
||||
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
|
||||
err.span_suggestion(
|
||||
pat.span,
|
||||
"you can probably remove the explicit borrow",
|
||||
snippet,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {} // don't provide suggestions in other cases #55175
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_dereferencable(&self, span: Span, expected: Ty<'tcx>, inner: &hir::Pat) -> bool {
|
||||
if let PatKind::Binding(..) = inner.node {
|
||||
if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true) {
|
||||
|
|
|
@ -23,7 +23,7 @@ LL | let &&x = &1isize as &T;
|
|||
| ^^
|
||||
| |
|
||||
| expected trait T, found reference
|
||||
| help: you can probaly remove the explicit borrow: `x`
|
||||
| help: you can probably remove the explicit borrow: `x`
|
||||
|
|
||||
= note: expected type `dyn T`
|
||||
found type `&_`
|
||||
|
@ -35,7 +35,7 @@ LL | let &&&x = &(&1isize as &T);
|
|||
| ^^
|
||||
| |
|
||||
| expected trait T, found reference
|
||||
| help: you can probaly remove the explicit borrow: `x`
|
||||
| help: you can probably remove the explicit borrow: `x`
|
||||
|
|
||||
= note: expected type `dyn T`
|
||||
found type `&_`
|
||||
|
|
|
@ -15,7 +15,7 @@ LL | fn agh(&&bar: &u32) {
|
|||
| ^^^^
|
||||
| |
|
||||
| expected u32, found reference
|
||||
| help: you can probaly remove the explicit borrow: `bar`
|
||||
| help: you can probably remove the explicit borrow: `bar`
|
||||
|
|
||||
= note: expected type `u32`
|
||||
found type `&_`
|
||||
|
|
|
@ -5,7 +5,7 @@ LL | [&v] => {},
|
|||
| ^^
|
||||
| |
|
||||
| expected i32, found reference
|
||||
| help: you can probaly remove the explicit borrow: `v`
|
||||
| help: you can probably remove the explicit borrow: `v`
|
||||
|
|
||||
= note: expected type `i32`
|
||||
found type `&_`
|
||||
|
|
Loading…
Add table
Reference in a new issue