Skip to content

Commit

Permalink
suggestion-diagnostics: as_ref improve snippet
Browse files Browse the repository at this point in the history
Improve the code snippet suggested in suggestion-diagnostics when
suggesting the use of as_ref.
  • Loading branch information
dlrobertson committed Jan 8, 2019
1 parent b8c8f0b commit 94fe935
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc::traits::ObligationCause;

use syntax::ast;
use syntax::util::parser::PREC_POSTFIX;
use syntax::source_map::SourceMap;
use syntax_pos::Span;
use rustc::hir;
use rustc::hir::def::Def;
Expand Down Expand Up @@ -191,7 +192,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
/// ```
/// opt.map(|arg| { takes_ref(arg) });
/// ```
fn can_use_as_ref(&self, expr: &hir::Expr) -> Option<(Span, &'static str, String)> {
fn can_use_as_ref(&self, expr: &hir::Expr, cm: &SourceMap) -> Option<(Span, &'static str, String)> {
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
if let hir::def::Def::Local(id) = path.def {
let parent = self.tcx.hir().get_parent_node(id);
Expand All @@ -214,10 +215,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self_ty.starts_with("std::option::Option") ||
self_ty.starts_with("std::result::Result")
) && (name == "map" || name == "and_then");
if is_as_ref_able {
return Some((span.shrink_to_lo(),
"consider using `as_ref` instead",
"as_ref().".into()));
match (is_as_ref_able, cm.span_to_snippet(*span)) {
(true, Ok(src)) => {
return Some((span.shrink_to_lo(), "consider using `as_ref` instead",
format!("as_ref().{}", src)));
},
_ => ()
}
}
}
Expand Down Expand Up @@ -315,7 +318,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
src
};

if let Some(sugg) = self.can_use_as_ref(expr) {
if let Some(sugg) = self.can_use_as_ref(expr, &cm) {
return Some(sugg);
}
return Some(match mutability {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/suggestions/as-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | opt.map(|arg| takes_ref(arg));
| - ^^^ expected &Foo, found struct `Foo`
| |
| help: consider using `as_ref` instead: `as_ref().`
| help: consider using `as_ref` instead: `as_ref().map`
|
= note: expected type `&Foo`
found type `Foo`
Expand All @@ -15,7 +15,7 @@ error[E0308]: mismatched types
LL | opt.and_then(|arg| Some(takes_ref(arg)));
| - ^^^ expected &Foo, found struct `Foo`
| |
| help: consider using `as_ref` instead: `as_ref().`
| help: consider using `as_ref` instead: `as_ref().and_then`
|
= note: expected type `&Foo`
found type `Foo`
Expand All @@ -26,7 +26,7 @@ error[E0308]: mismatched types
LL | opt.map(|arg| takes_ref(arg));
| - ^^^ expected &Foo, found struct `Foo`
| |
| help: consider using `as_ref` instead: `as_ref().`
| help: consider using `as_ref` instead: `as_ref().map`
|
= note: expected type `&Foo`
found type `Foo`
Expand All @@ -37,7 +37,7 @@ error[E0308]: mismatched types
LL | opt.and_then(|arg| Ok(takes_ref(arg)));
| - ^^^ expected &Foo, found struct `Foo`
| |
| help: consider using `as_ref` instead: `as_ref().`
| help: consider using `as_ref` instead: `as_ref().and_then`
|
= note: expected type `&Foo`
found type `Foo`
Expand Down

0 comments on commit 94fe935

Please sign in to comment.