Improve redundant_slicing lint
* Lint when slicing triggers auto-deref * Lint when slicing returns the same type as dereferencing
This commit is contained in:
parent
e2dc8ca080
commit
faeeef3b9c
4 changed files with 134 additions and 31 deletions
40
tests/ui/redundant_slicing.fixed
Normal file
40
tests/ui/redundant_slicing.fixed
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::redundant_slicing)]
|
||||
|
||||
fn main() {
|
||||
let slice: &[u32] = &[0];
|
||||
let _ = slice; // Redundant slice
|
||||
|
||||
let v = vec![0];
|
||||
let _ = &*v; // Deref instead of slice
|
||||
let _ = (&*v); // Outer borrow is redundant
|
||||
|
||||
static S: &[u8] = &[0, 1, 2];
|
||||
let err = &mut &*S; // Should reborrow instead of slice
|
||||
|
||||
let mut vec = vec![0];
|
||||
let mut_slice = &mut *vec; // Deref instead of slice
|
||||
let _ = &mut *mut_slice; // Should reborrow instead of slice
|
||||
|
||||
let ref_vec = &vec;
|
||||
let _ = &**ref_vec; // Deref instead of slice
|
||||
|
||||
macro_rules! m {
|
||||
($e:expr) => {
|
||||
$e
|
||||
};
|
||||
}
|
||||
let _ = slice;
|
||||
|
||||
macro_rules! m2 {
|
||||
($e:expr) => {
|
||||
&$e[..]
|
||||
};
|
||||
}
|
||||
let _ = m2!(slice); // Don't lint in a macro
|
||||
|
||||
let slice_ref = &slice;
|
||||
let _ = *slice_ref; // Deref instead of slice
|
||||
}
|
||||
|
|
@ -1,21 +1,26 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::redundant_slicing)]
|
||||
|
||||
fn main() {
|
||||
let slice: &[u32] = &[0];
|
||||
let _ = &slice[..];
|
||||
let _ = &slice[..]; // Redundant slice
|
||||
|
||||
let v = vec![0];
|
||||
let _ = &v[..]; // Changes the type
|
||||
let _ = &(&v[..])[..]; // Outer borrow is redundant
|
||||
let _ = &v[..]; // Deref instead of slice
|
||||
let _ = &(&*v)[..]; // Outer borrow is redundant
|
||||
|
||||
static S: &[u8] = &[0, 1, 2];
|
||||
let err = &mut &S[..]; // Should reborrow instead of slice
|
||||
|
||||
let mut vec = vec![0];
|
||||
let mut_slice = &mut *vec;
|
||||
let mut_slice = &mut vec[..]; // Deref instead of slice
|
||||
let _ = &mut mut_slice[..]; // Should reborrow instead of slice
|
||||
|
||||
let ref_vec = &vec;
|
||||
let _ = &ref_vec[..]; // Deref instead of slice
|
||||
|
||||
macro_rules! m {
|
||||
($e:expr) => {
|
||||
$e
|
||||
|
|
@ -29,4 +34,7 @@ fn main() {
|
|||
};
|
||||
}
|
||||
let _ = m2!(slice); // Don't lint in a macro
|
||||
|
||||
let slice_ref = &slice;
|
||||
let _ = &slice_ref[..]; // Deref instead of slice
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,34 +1,58 @@
|
|||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:6:13
|
||||
--> $DIR/redundant_slicing.rs:8:13
|
||||
|
|
||||
LL | let _ = &slice[..];
|
||||
LL | let _ = &slice[..]; // Redundant slice
|
||||
| ^^^^^^^^^^ help: use the original value instead: `slice`
|
||||
|
|
||||
= note: `-D clippy::redundant-slicing` implied by `-D warnings`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:10:13
|
||||
--> $DIR/redundant_slicing.rs:11:13
|
||||
|
|
||||
LL | let _ = &(&v[..])[..]; // Outer borrow is redundant
|
||||
| ^^^^^^^^^^^^^ help: use the original value instead: `(&v[..])`
|
||||
LL | let _ = &v[..]; // Deref instead of slice
|
||||
| ^^^^^^ help: dereference the original value instead: `&*v`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:13:20
|
||||
--> $DIR/redundant_slicing.rs:12:13
|
||||
|
|
||||
LL | let _ = &(&*v)[..]; // Outer borrow is redundant
|
||||
| ^^^^^^^^^^ help: use the original value instead: `(&*v)`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:15:20
|
||||
|
|
||||
LL | let err = &mut &S[..]; // Should reborrow instead of slice
|
||||
| ^^^^^^ help: reborrow the original value instead: `&*S`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:17:13
|
||||
--> $DIR/redundant_slicing.rs:18:21
|
||||
|
|
||||
LL | let mut_slice = &mut vec[..]; // Deref instead of slice
|
||||
| ^^^^^^^^^^^^ help: dereference the original value instead: `&mut *vec`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:19:13
|
||||
|
|
||||
LL | let _ = &mut mut_slice[..]; // Should reborrow instead of slice
|
||||
| ^^^^^^^^^^^^^^^^^^ help: reborrow the original value instead: `&mut *mut_slice`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:24:13
|
||||
--> $DIR/redundant_slicing.rs:22:13
|
||||
|
|
||||
LL | let _ = &ref_vec[..]; // Deref instead of slice
|
||||
| ^^^^^^^^^^^^ help: dereference the original value instead: `&**ref_vec`
|
||||
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:29:13
|
||||
|
|
||||
LL | let _ = &m!(slice)[..];
|
||||
| ^^^^^^^^^^^^^^ help: use the original value instead: `slice`
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
error: redundant slicing of the whole range
|
||||
--> $DIR/redundant_slicing.rs:39:13
|
||||
|
|
||||
LL | let _ = &slice_ref[..]; // Deref instead of slice
|
||||
| ^^^^^^^^^^^^^^ help: dereference the original value instead: `*slice_ref`
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue