@@ -42,7 +42,31 @@ declare_clippy_lint! {
4242 "redundant slicing of the whole range of a type"
4343}
4444
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 = &vec[..];
57+ /// ```
58+ /// Use instead:
59+ /// ```rust
60+ /// let vec = vec![1, 2, 3];
61+ /// let slice = &*vec;
62+ /// ```
63+ #[ clippy:: version = "1.60.0" ]
64+ pub DEREF_BY_SLICING ,
65+ restriction,
66+ "slicing instead of dereferencing"
67+ }
68+
69+ declare_lint_pass ! ( RedundantSlicing => [ REDUNDANT_SLICING , DEREF_BY_SLICING ] ) ;
4670
4771impl < ' tcx > LateLintPass < ' tcx > for RedundantSlicing {
4872 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
@@ -65,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
6589 } ) ;
6690 let mut app = Applicability :: MachineApplicable ;
6791
68- let ( help, sugg) = if expr_ty == indexed_ty {
92+ let ( lint , msg , help, sugg) = if expr_ty == indexed_ty {
6993 if expr_ref_count > indexed_ref_count {
7094 // Indexing takes self by reference and can't return a reference to that
7195 // reference as it's a local variable. The only way this could happen is if
@@ -103,7 +127,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
103127 format!( "{}{}{}" , reborrow_str, "*" . repeat( deref_count) , snip)
104128 } ;
105129
106- ( help_str, sugg)
130+ ( REDUNDANT_SLICING , "redundant slicing of the whole range" , help_str, sugg)
107131 } else if let Some ( target_id) = cx. tcx. lang_items( ) . deref_target( ) {
108132 if let Ok ( deref_ty) = cx. tcx. try_normalize_erasing_regions(
109133 cx. param_env,
@@ -116,7 +140,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
116140 } else {
117141 format!( "&{}{}*{}" , mutability. prefix_str( ) , "*" . repeat( indexed_ref_count) , snip)
118142 } ;
119- ( "dereference the original value instead" , sugg)
143+ ( DEREF_BY_SLICING , "slicing when dereferencing would work" , "dereference the original value instead" , sugg)
120144 } else {
121145 return ;
122146 }
@@ -129,9 +153,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
129153
130154 span_lint_and_sugg(
131155 cx,
132- REDUNDANT_SLICING ,
156+ lint ,
133157 expr. span,
134- "redundant slicing of the whole range" ,
158+ msg ,
135159 help,
136160 sugg,
137161 app,
0 commit comments