@@ -6,7 +6,7 @@ use if_chain::if_chain;
6
6
use rustc_ast:: util:: parser:: PREC_PREFIX ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: { BorrowKind , Expr , ExprKind , LangItem , Mutability } ;
9
- use rustc_lint:: { LateContext , LateLintPass } ;
9
+ use rustc_lint:: { LateContext , LateLintPass , Lint } ;
10
10
use rustc_middle:: ty:: adjustment:: { Adjust , AutoBorrow , AutoBorrowMutability } ;
11
11
use rustc_middle:: ty:: { subst:: GenericArg , TyS } ;
12
12
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -44,7 +44,7 @@ declare_clippy_lint! {
44
44
45
45
declare_clippy_lint ! {
46
46
/// ### 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
48
48
/// value.
49
49
///
50
50
/// ### Why is this bad?
@@ -68,6 +68,9 @@ declare_clippy_lint! {
68
68
69
69
declare_lint_pass ! ( RedundantSlicing => [ REDUNDANT_SLICING , DEREF_BY_SLICING ] ) ;
70
70
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
+
71
74
impl < ' tcx > LateLintPass < ' tcx > for RedundantSlicing {
72
75
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
73
76
if expr. span . from_expansion ( ) {
@@ -89,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
89
92
} ) ;
90
93
let mut app = Applicability :: MachineApplicable ;
91
94
92
- let ( lint, msg, help, sugg) = if TyS :: same_type( expr_ty, indexed_ty) {
95
+ let ( ( lint, msg) , help, sugg) = if TyS :: same_type( expr_ty, indexed_ty) {
93
96
if expr_ref_count > indexed_ref_count {
94
97
// Indexing takes self by reference and can't return a reference to that
95
98
// 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 {
100
103
}
101
104
let deref_count = indexed_ref_count - expr_ref_count;
102
105
103
- let ( reborrow_str, help_str) = if mutability == Mutability :: Mut {
106
+ let ( lint , reborrow_str, help_str) = if mutability == Mutability :: Mut {
104
107
// 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" )
106
109
} else if matches!(
107
110
parent_expr,
108
111
Some ( Expr {
@@ -113,11 +116,11 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
113
116
matches!( a. kind, Adjust :: Borrow ( AutoBorrow :: Ref ( _, AutoBorrowMutability :: Mut { .. } ) ) )
114
117
} ) {
115
118
// 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" )
117
120
} else if deref_count != 0 {
118
- ( "" , "dereference the original value instead" )
121
+ ( DEREF_BY_SLICING_LINT , "" , "dereference the original value instead" )
119
122
} else {
120
- ( "" , "use the original value instead" )
123
+ ( REDUNDANT_SLICING_LINT , "" , "use the original value instead" )
121
124
} ;
122
125
123
126
let snip = snippet_with_context( cx, indexed. span, ctxt, ".." , & mut app) . 0 ;
@@ -127,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
127
130
format!( "{}{}{}" , reborrow_str, "*" . repeat( deref_count) , snip)
128
131
} ;
129
132
130
- ( REDUNDANT_SLICING , "redundant slicing of the whole range" , help_str, sugg)
133
+ ( lint , help_str, sugg)
131
134
} else if let Some ( target_id) = cx. tcx. lang_items( ) . deref_target( ) {
132
135
if let Ok ( deref_ty) = cx. tcx. try_normalize_erasing_regions(
133
136
cx. param_env,
@@ -140,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
140
143
} else {
141
144
format!( "&{}{}*{}" , mutability. prefix_str( ) , "*" . repeat( indexed_ref_count) , snip)
142
145
} ;
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)
144
147
} else {
145
148
return ;
146
149
}
0 commit comments