@@ -6,7 +6,7 @@ use if_chain::if_chain;
66use rustc_ast:: util:: parser:: PREC_PREFIX ;
77use rustc_errors:: Applicability ;
88use rustc_hir:: { BorrowKind , Expr , ExprKind , LangItem , Mutability } ;
9- use rustc_lint:: { LateContext , LateLintPass } ;
9+ use rustc_lint:: { LateContext , LateLintPass , Lint } ;
1010use rustc_middle:: ty:: adjustment:: { Adjust , AutoBorrow , AutoBorrowMutability } ;
1111use rustc_middle:: ty:: subst:: GenericArg ;
1212use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -44,7 +44,7 @@ declare_clippy_lint! {
4444
4545declare_clippy_lint ! {
4646 /// ### What it does
47- /// Checks for slicing expression which are equivalent to dereferencing the
47+ /// Checks for slicing expressions which are equivalent to dereferencing the
4848 /// value.
4949 ///
5050 /// ### Why is this bad?
@@ -68,6 +68,9 @@ declare_clippy_lint! {
6868
6969declare_lint_pass ! ( RedundantSlicing => [ REDUNDANT_SLICING , DEREF_BY_SLICING ] ) ;
7070
71+ static REDUNDANT_SLICING_LINT : ( & Lint , & str ) = ( REDUNDANT_SLICING , "redundant slicing of the whole range" ) ;
72+ static DEREF_BY_SLICING_LINT : ( & Lint , & str ) = ( DEREF_BY_SLICING , "slicing when dereferencing would work" ) ;
73+
7174impl < ' tcx > LateLintPass < ' tcx > for RedundantSlicing {
7275 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
7376 if expr. span . from_expansion ( ) {
@@ -89,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
8992 } ) ;
9093 let mut app = Applicability :: MachineApplicable ;
9194
92- let ( lint, msg, help, sugg) = if expr_ty == indexed_ty {
95+ let ( ( lint, msg) , help, sugg) = if expr_ty == indexed_ty {
9396 if expr_ref_count > indexed_ref_count {
9497 // Indexing takes self by reference and can't return a reference to that
9598 // reference as it's a local variable. The only way this could happen is if
@@ -100,9 +103,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
100103 }
101104 let deref_count = indexed_ref_count - expr_ref_count;
102105
103- let ( reborrow_str, help_str) = if mutability == Mutability :: Mut {
106+ let ( lint , reborrow_str, help_str) = if mutability == Mutability :: Mut {
104107 // The slice was used to reborrow the mutable reference.
105- ( "&mut *" , "reborrow the original value instead" )
108+ ( DEREF_BY_SLICING_LINT , "&mut *" , "reborrow the original value instead" )
106109 } else if matches!(
107110 parent_expr,
108111 Some ( Expr {
@@ -113,11 +116,11 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
113116 matches!( a. kind, Adjust :: Borrow ( AutoBorrow :: Ref ( _, AutoBorrowMutability :: Mut { .. } ) ) )
114117 } ) {
115118 // The slice was used to make a temporary reference.
116- ( "&*" , "reborrow the original value instead" )
119+ ( DEREF_BY_SLICING_LINT , "&*" , "reborrow the original value instead" )
117120 } else if deref_count != 0 {
118- ( "" , "dereference the original value instead" )
121+ ( DEREF_BY_SLICING_LINT , "" , "dereference the original value instead" )
119122 } else {
120- ( "" , "use the original value instead" )
123+ ( REDUNDANT_SLICING_LINT , "" , "use the original value instead" )
121124 } ;
122125
123126 let snip = snippet_with_context( cx, indexed. span, ctxt, ".." , & mut app) . 0 ;
@@ -127,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
127130 format!( "{}{}{}" , reborrow_str, "*" . repeat( deref_count) , snip)
128131 } ;
129132
130- ( REDUNDANT_SLICING , "redundant slicing of the whole range" , help_str, sugg)
133+ ( lint , help_str, sugg)
131134 } else if let Some ( target_id) = cx. tcx. lang_items( ) . deref_target( ) {
132135 if let Ok ( deref_ty) = cx. tcx. try_normalize_erasing_regions(
133136 cx. param_env,
@@ -140,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
140143 } else {
141144 format!( "&{}{}*{}" , mutability. prefix_str( ) , "*" . repeat( indexed_ref_count) , snip)
142145 } ;
143- ( DEREF_BY_SLICING , "slicing when dereferencing would work" , "dereference the original value instead" , sugg)
146+ ( DEREF_BY_SLICING_LINT , "dereference the original value instead" , sugg)
144147 } else {
145148 return ;
146149 }
0 commit comments