@@ -145,6 +145,7 @@ enum State {
145
145
DerefedBorrow {
146
146
count : usize ,
147
147
required_precedence : i8 ,
148
+ msg : & ' static str ,
148
149
} ,
149
150
}
150
151
@@ -259,20 +260,25 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
259
260
// None => y
260
261
// };
261
262
// }
262
- let ( required_refs, required_precedence) = if is_auto_borrow_position ( parent, expr. hir_id ) {
263
- ( 1 , PREC_POSTFIX )
263
+ let deref_msg =
264
+ "this expression creates a reference which is immediately dereferenced by the compiler" ;
265
+ let borrow_msg = "this expression borrows a value the compiler would automatically borrow" ;
266
+
267
+ let ( required_refs, required_precedence, msg) = if is_auto_borrow_position ( parent, expr. hir_id )
268
+ {
269
+ ( 1 , PREC_POSTFIX , if deref_count == 1 { borrow_msg } else { deref_msg } )
264
270
} else if let Some ( & Adjust :: Borrow ( AutoBorrow :: Ref ( _, mutability) ) ) =
265
271
next_adjust. map ( |a| & a. kind )
266
272
{
267
273
if matches ! ( mutability, AutoBorrowMutability :: Mut { .. } )
268
274
&& !is_auto_reborrow_position ( parent)
269
275
{
270
- ( 3 , 0 )
276
+ ( 3 , 0 , deref_msg )
271
277
} else {
272
- ( 2 , 0 )
278
+ ( 2 , 0 , deref_msg )
273
279
}
274
280
} else {
275
- ( 2 , 0 )
281
+ ( 2 , 0 , deref_msg )
276
282
} ;
277
283
278
284
if deref_count >= required_refs {
@@ -282,6 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
282
288
// can't be removed without breaking the code. See earlier comment.
283
289
count : deref_count - required_refs,
284
290
required_precedence,
291
+ msg,
285
292
} ,
286
293
StateData { span : expr. span } ,
287
294
) ) ;
@@ -319,6 +326,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
319
326
State :: DerefedBorrow {
320
327
count,
321
328
required_precedence,
329
+ msg,
322
330
} ,
323
331
data,
324
332
) ) ,
@@ -328,6 +336,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
328
336
State :: DerefedBorrow {
329
337
count : count - 1 ,
330
338
required_precedence,
339
+ msg,
331
340
} ,
332
341
data,
333
342
) ) ;
@@ -624,15 +633,17 @@ fn report(cx: &LateContext<'_>, expr: &Expr<'_>, state: State, data: StateData)
624
633
) ;
625
634
} ,
626
635
State :: DerefedBorrow {
627
- required_precedence, ..
636
+ required_precedence,
637
+ msg,
638
+ ..
628
639
} => {
629
640
let mut app = Applicability :: MachineApplicable ;
630
641
let snip = snippet_with_context ( cx, expr. span , data. span . ctxt ( ) , ".." , & mut app) . 0 ;
631
642
span_lint_and_sugg (
632
643
cx,
633
644
NEEDLESS_BORROW ,
634
645
data. span ,
635
- "this expression creates a reference which is immediately dereferenced by the compiler" ,
646
+ msg ,
636
647
"change this to" ,
637
648
if required_precedence > expr. precedence ( ) . order ( ) && !has_enclosing_paren ( & snip) {
638
649
format ! ( "({})" , snip)
0 commit comments