Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Lint when slicing triggers auto-deref * Lint when slicing returns the same type as dereferencing
- Loading branch information
Showing
4 changed files
with
134 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|