@@ -134,10 +134,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
134134 }
135135 }
136136
137- if let Some ( suggestion) = self . check_ref ( expr,
138- checked_ty,
139- expected) {
140- err. help ( & suggestion) ;
137+ if let Some ( ( msg, suggestion) ) = self . check_ref ( expr, checked_ty, expected) {
138+ err. span_suggestion ( expr. span , msg, suggestion) ;
141139 } else {
142140 let mode = probe:: Mode :: MethodCall ;
143141 let suggestions = self . probe_for_return_type ( syntax_pos:: DUMMY_SP ,
@@ -214,15 +212,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
214212 expr : & hir:: Expr ,
215213 checked_ty : Ty < ' tcx > ,
216214 expected : Ty < ' tcx > )
217- -> Option < String > {
215+ -> Option < ( & ' static str , String ) > {
218216 match ( & expected. sty , & checked_ty. sty ) {
219217 ( & ty:: TyRef ( _, exp) , & ty:: TyRef ( _, check) ) => match ( & exp. ty . sty , & check. ty . sty ) {
220218 ( & ty:: TyStr , & ty:: TyArray ( arr, _) ) |
221219 ( & ty:: TyStr , & ty:: TySlice ( arr) ) if arr == self . tcx . types . u8 => {
222220 if let hir:: ExprLit ( _) = expr. node {
223221 let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
224222 if let Ok ( src) = self . tcx . sess . codemap ( ) . span_to_snippet ( sp) {
225- return Some ( format ! ( "try `{}`" , & src[ 1 ..] ) ) ;
223+ return Some ( ( "consider removing the leading `b`" ,
224+ src[ 1 ..] . to_string ( ) ) ) ;
226225 }
227226 }
228227 None
@@ -232,7 +231,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
232231 if let hir:: ExprLit ( _) = expr. node {
233232 let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
234233 if let Ok ( src) = self . tcx . sess . codemap ( ) . span_to_snippet ( sp) {
235- return Some ( format ! ( "try `b{}`" , src) ) ;
234+ return Some ( ( "consider adding a leading `b`" ,
235+ format ! ( "b{}" , src) ) ) ;
236236 }
237237 }
238238 None
@@ -260,12 +260,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
260260 // Use the callsite's span if this is a macro call. #41858
261261 let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
262262 if let Ok ( src) = self . tcx . sess . codemap ( ) . span_to_snippet ( sp) {
263- return Some ( format ! ( "try with `{}{}`" ,
264- match mutability. mutbl {
265- hir:: Mutability :: MutMutable => "&mut " ,
266- hir:: Mutability :: MutImmutable => "&" ,
267- } ,
268- & src) ) ;
263+ return Some ( match mutability. mutbl {
264+ hir:: Mutability :: MutMutable => {
265+ ( "consider mutably borrowing here" , format ! ( "&mut {}" , src) )
266+ }
267+ hir:: Mutability :: MutImmutable => {
268+ ( "consider borrowing here" , format ! ( "&{}" , src) )
269+ }
270+ } ) ;
269271 }
270272 }
271273 None
@@ -284,7 +286,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
284286 // Maybe remove `&`?
285287 hir:: ExprAddrOf ( _, ref expr) => {
286288 if let Ok ( code) = self . tcx . sess . codemap ( ) . span_to_snippet ( expr. span ) {
287- return Some ( format ! ( "try with `{}`" , code) ) ;
289+ return Some ( ( "consider removing the borrow" ,
290+ code) ) ;
288291 }
289292 }
290293
@@ -295,7 +298,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
295298 expr. span ) {
296299 let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
297300 if let Ok ( code) = self . tcx . sess . codemap ( ) . span_to_snippet ( sp) {
298- return Some ( format ! ( "try with `*{}`" , code) ) ;
301+ return Some ( ( "consider dereferencing the borrow" ,
302+ format ! ( "*{}" , code) ) ) ;
299303 }
300304 }
301305 } ,
0 commit comments