Skip to content

Commit 94fe935

Browse files
committed
suggestion-diagnostics: as_ref improve snippet
Improve the code snippet suggested in suggestion-diagnostics when suggesting the use of as_ref.
1 parent b8c8f0b commit 94fe935

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc::traits::ObligationCause;
44

55
use syntax::ast;
66
use syntax::util::parser::PREC_POSTFIX;
7+
use syntax::source_map::SourceMap;
78
use syntax_pos::Span;
89
use rustc::hir;
910
use rustc::hir::def::Def;
@@ -191,7 +192,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
191192
/// ```
192193
/// opt.map(|arg| { takes_ref(arg) });
193194
/// ```
194-
fn can_use_as_ref(&self, expr: &hir::Expr) -> Option<(Span, &'static str, String)> {
195+
fn can_use_as_ref(&self, expr: &hir::Expr, cm: &SourceMap) -> Option<(Span, &'static str, String)> {
195196
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
196197
if let hir::def::Def::Local(id) = path.def {
197198
let parent = self.tcx.hir().get_parent_node(id);
@@ -214,10 +215,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
214215
self_ty.starts_with("std::option::Option") ||
215216
self_ty.starts_with("std::result::Result")
216217
) && (name == "map" || name == "and_then");
217-
if is_as_ref_able {
218-
return Some((span.shrink_to_lo(),
219-
"consider using `as_ref` instead",
220-
"as_ref().".into()));
218+
match (is_as_ref_able, cm.span_to_snippet(*span)) {
219+
(true, Ok(src)) => {
220+
return Some((span.shrink_to_lo(), "consider using `as_ref` instead",
221+
format!("as_ref().{}", src)));
222+
},
223+
_ => ()
221224
}
222225
}
223226
}
@@ -315,7 +318,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
315318
src
316319
};
317320

318-
if let Some(sugg) = self.can_use_as_ref(expr) {
321+
if let Some(sugg) = self.can_use_as_ref(expr, &cm) {
319322
return Some(sugg);
320323
}
321324
return Some(match mutability {

src/test/ui/suggestions/as-ref.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | opt.map(|arg| takes_ref(arg));
55
| - ^^^ expected &Foo, found struct `Foo`
66
| |
7-
| help: consider using `as_ref` instead: `as_ref().`
7+
| help: consider using `as_ref` instead: `as_ref().map`
88
|
99
= note: expected type `&Foo`
1010
found type `Foo`
@@ -15,7 +15,7 @@ error[E0308]: mismatched types
1515
LL | opt.and_then(|arg| Some(takes_ref(arg)));
1616
| - ^^^ expected &Foo, found struct `Foo`
1717
| |
18-
| help: consider using `as_ref` instead: `as_ref().`
18+
| help: consider using `as_ref` instead: `as_ref().and_then`
1919
|
2020
= note: expected type `&Foo`
2121
found type `Foo`
@@ -26,7 +26,7 @@ error[E0308]: mismatched types
2626
LL | opt.map(|arg| takes_ref(arg));
2727
| - ^^^ expected &Foo, found struct `Foo`
2828
| |
29-
| help: consider using `as_ref` instead: `as_ref().`
29+
| help: consider using `as_ref` instead: `as_ref().map`
3030
|
3131
= note: expected type `&Foo`
3232
found type `Foo`
@@ -37,7 +37,7 @@ error[E0308]: mismatched types
3737
LL | opt.and_then(|arg| Ok(takes_ref(arg)));
3838
| - ^^^ expected &Foo, found struct `Foo`
3939
| |
40-
| help: consider using `as_ref` instead: `as_ref().`
40+
| help: consider using `as_ref` instead: `as_ref().and_then`
4141
|
4242
= note: expected type `&Foo`
4343
found type `Foo`

0 commit comments

Comments
 (0)