@@ -134,10 +134,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
134
134
}
135
135
}
136
136
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) ;
141
139
} else {
142
140
let mode = probe:: Mode :: MethodCall ;
143
141
let suggestions = self . probe_for_return_type ( syntax_pos:: DUMMY_SP ,
@@ -214,15 +212,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
214
212
expr : & hir:: Expr ,
215
213
checked_ty : Ty < ' tcx > ,
216
214
expected : Ty < ' tcx > )
217
- -> Option < String > {
215
+ -> Option < ( & ' static str , String ) > {
218
216
match ( & expected. sty , & checked_ty. sty ) {
219
217
( & ty:: TyRef ( _, exp) , & ty:: TyRef ( _, check) ) => match ( & exp. ty . sty , & check. ty . sty ) {
220
218
( & ty:: TyStr , & ty:: TyArray ( arr, _) ) |
221
219
( & ty:: TyStr , & ty:: TySlice ( arr) ) if arr == self . tcx . types . u8 => {
222
220
if let hir:: ExprLit ( _) = expr. node {
223
221
let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
224
222
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 ( ) ) ) ;
226
225
}
227
226
}
228
227
None
@@ -232,7 +231,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
232
231
if let hir:: ExprLit ( _) = expr. node {
233
232
let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
234
233
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) ) ) ;
236
236
}
237
237
}
238
238
None
@@ -260,12 +260,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
260
260
// Use the callsite's span if this is a macro call. #41858
261
261
let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
262
262
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 using a mutable borrow" , format ! ( "&mut {}" , src) )
266
+ }
267
+ hir:: Mutability :: MutImmutable => {
268
+ ( "consider using a borrow" , format ! ( "&{}" , src) )
269
+ }
270
+ } ) ;
269
271
}
270
272
}
271
273
None
@@ -284,7 +286,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
284
286
// Maybe remove `&`?
285
287
hir:: ExprAddrOf ( _, ref expr) => {
286
288
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) ) ;
288
291
}
289
292
}
290
293
@@ -295,7 +298,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
295
298
expr. span ) {
296
299
let sp = self . sess ( ) . codemap ( ) . call_span_if_macro ( expr. span ) ;
297
300
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) ) ) ;
299
303
}
300
304
}
301
305
} ,
0 commit comments