@@ -4,6 +4,7 @@ use rustc::traits::ObligationCause;
4
4
5
5
use syntax:: ast;
6
6
use syntax:: util:: parser:: PREC_POSTFIX ;
7
+ use syntax:: source_map:: SourceMap ;
7
8
use syntax_pos:: Span ;
8
9
use rustc:: hir;
9
10
use rustc:: hir:: def:: Def ;
@@ -191,7 +192,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
191
192
/// ```
192
193
/// opt.map(|arg| { takes_ref(arg) });
193
194
/// ```
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 ) > {
195
196
if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, ref path) ) = expr. node {
196
197
if let hir:: def:: Def :: Local ( id) = path. def {
197
198
let parent = self . tcx . hir ( ) . get_parent_node ( id) ;
@@ -214,10 +215,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
214
215
self_ty. starts_with ( "std::option::Option" ) ||
215
216
self_ty. starts_with ( "std::result::Result" )
216
217
) && ( 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
+ _ => ( )
221
224
}
222
225
}
223
226
}
@@ -315,7 +318,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
315
318
src
316
319
} ;
317
320
318
- if let Some ( sugg) = self . can_use_as_ref ( expr) {
321
+ if let Some ( sugg) = self . can_use_as_ref ( expr, & cm ) {
319
322
return Some ( sugg) ;
320
323
}
321
324
return Some ( match mutability {
0 commit comments