@@ -42,7 +42,31 @@ declare_clippy_lint! {
42
42
"redundant slicing of the whole range of a type"
43
43
}
44
44
45
- declare_lint_pass ! ( RedundantSlicing => [ REDUNDANT_SLICING ] ) ;
45
+ declare_clippy_lint ! {
46
+ /// ### What it does
47
+ /// Checks for slicing expression which are equivalent to dereferencing the
48
+ /// value.
49
+ ///
50
+ /// ### Why is this bad?
51
+ /// Some people may prefer to dereference rather than slice.
52
+ ///
53
+ /// ### Example
54
+ /// ```rust
55
+ /// let vec = vec![1, 2, 3];
56
+ /// let slice = &v[..];
57
+ /// ```
58
+ /// Use instead:
59
+ /// ```rust
60
+ /// let vec = vec![1, 2, 3];
61
+ /// let slice = &*v;
62
+ /// ```
63
+ #[ clippy:: version = "1.59.0" ]
64
+ pub SLICE_AS_DEREF ,
65
+ restriction,
66
+ "slicing instead of dereferencing"
67
+ }
68
+
69
+ declare_lint_pass ! ( RedundantSlicing => [ REDUNDANT_SLICING , SLICE_AS_DEREF ] ) ;
46
70
47
71
impl LateLintPass < ' _ > for RedundantSlicing {
48
72
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
@@ -65,7 +89,7 @@ impl LateLintPass<'_> for RedundantSlicing {
65
89
} ) ;
66
90
let mut app = Applicability :: MachineApplicable ;
67
91
68
- let ( help, sugg) = if TyS :: same_type( expr_ty, indexed_ty) {
92
+ let ( lint , msg , help, sugg) = if TyS :: same_type( expr_ty, indexed_ty) {
69
93
if expr_ref_count > indexed_ref_count {
70
94
// Indexing takes self by reference and can't return a reference to that
71
95
// reference as it's a local variable. The only way this could happen is if
@@ -103,7 +127,7 @@ impl LateLintPass<'_> for RedundantSlicing {
103
127
format!( "{}{}{}" , reborrow_str, "*" . repeat( deref_count) , snip)
104
128
} ;
105
129
106
- ( help_str, sugg)
130
+ ( REDUNDANT_SLICING , "redundant slicing of the whole range" , help_str, sugg)
107
131
} else if let Some ( target_id) = cx. tcx. lang_items( ) . deref_target( ) {
108
132
if let Ok ( deref_ty) = cx. tcx. try_normalize_erasing_regions(
109
133
cx. param_env,
@@ -116,7 +140,7 @@ impl LateLintPass<'_> for RedundantSlicing {
116
140
} else {
117
141
format!( "&{}{}*{}" , mutability. prefix_str( ) , "*" . repeat( indexed_ref_count) , snip)
118
142
} ;
119
- ( "dereference the original value instead" , sugg)
143
+ ( SLICE_AS_DEREF , "slicing when dereferencing would work" , "dereference the original value instead" , sugg)
120
144
} else {
121
145
return ;
122
146
}
@@ -129,9 +153,9 @@ impl LateLintPass<'_> for RedundantSlicing {
129
153
130
154
span_lint_and_sugg(
131
155
cx,
132
- REDUNDANT_SLICING ,
156
+ lint ,
133
157
expr. span,
134
- "redundant slicing of the whole range" ,
158
+ msg ,
135
159
help,
136
160
sugg,
137
161
app,
0 commit comments