@@ -145,6 +145,7 @@ enum State {
145145 DerefedBorrow {
146146 count : usize ,
147147 required_precedence : i8 ,
148+ msg : & ' static str ,
148149 } ,
149150}
150151
@@ -259,20 +260,25 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
259260 // None => y
260261 // };
261262 // }
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 } )
264270 } else if let Some ( & Adjust :: Borrow ( AutoBorrow :: Ref ( _, mutability) ) ) =
265271 next_adjust. map ( |a| & a. kind )
266272 {
267273 if matches ! ( mutability, AutoBorrowMutability :: Mut { .. } )
268274 && !is_auto_reborrow_position ( parent)
269275 {
270- ( 3 , 0 )
276+ ( 3 , 0 , deref_msg )
271277 } else {
272- ( 2 , 0 )
278+ ( 2 , 0 , deref_msg )
273279 }
274280 } else {
275- ( 2 , 0 )
281+ ( 2 , 0 , deref_msg )
276282 } ;
277283
278284 if deref_count >= required_refs {
@@ -282,6 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
282288 // can't be removed without breaking the code. See earlier comment.
283289 count : deref_count - required_refs,
284290 required_precedence,
291+ msg,
285292 } ,
286293 StateData { span : expr. span } ,
287294 ) ) ;
@@ -319,6 +326,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
319326 State :: DerefedBorrow {
320327 count,
321328 required_precedence,
329+ msg,
322330 } ,
323331 data,
324332 ) ) ,
@@ -328,6 +336,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
328336 State :: DerefedBorrow {
329337 count : count - 1 ,
330338 required_precedence,
339+ msg,
331340 } ,
332341 data,
333342 ) ) ;
@@ -624,15 +633,17 @@ fn report(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data: Stat
624633 ) ;
625634 } ,
626635 State :: DerefedBorrow {
627- required_precedence, ..
636+ required_precedence,
637+ msg,
638+ ..
628639 } => {
629640 let mut app = Applicability :: MachineApplicable ;
630641 let snip = snippet_with_context ( cx, expr. span , data. span . ctxt ( ) , ".." , & mut app) . 0 ;
631642 span_lint_and_sugg (
632643 cx,
633644 NEEDLESS_BORROW ,
634645 data. span ,
635- "this expression creates a reference which is immediately dereferenced by the compiler" ,
646+ msg ,
636647 "change this to" ,
637648 if required_precedence > expr. precedence ( ) . order ( ) && !has_enclosing_paren ( & snip) {
638649 format ! ( "({})" , snip)
0 commit comments