forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#8218 - Jarcho:redundant_slicing_deref, r=cams…
…teffen Improve `redundant_slicing` lint fixes rust-lang#7972 fixes rust-lang#7257 This can supersede rust-lang#7976 changelog: Fix suggestion for `redundant_slicing` when re-borrowing for a method call changelog: New lint `deref_as_slicing`
- Loading branch information
Showing
10 changed files
with
295 additions
and
48 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
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
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,29 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::deref_by_slicing)] | ||
|
||
use std::io::Read; | ||
|
||
fn main() { | ||
let mut vec = vec![0]; | ||
let _ = &*vec; | ||
let _ = &mut *vec; | ||
|
||
let ref_vec = &mut vec; | ||
let _ = &**ref_vec; | ||
let mut_slice = &mut **ref_vec; | ||
let _ = &mut *mut_slice; // Err, re-borrows slice | ||
|
||
let s = String::new(); | ||
let _ = &*s; | ||
|
||
static S: &[u8] = &[0, 1, 2]; | ||
let _ = &mut &*S; // Err, re-borrows slice | ||
|
||
let slice: &[u32] = &[0u32, 1u32]; | ||
let slice_ref = &slice; | ||
let _ = *slice_ref; // Err, derefs slice | ||
|
||
let bytes: &[u8] = &[]; | ||
let _ = (&*bytes).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::deref_by_slicing)] | ||
|
||
use std::io::Read; | ||
|
||
fn main() { | ||
let mut vec = vec![0]; | ||
let _ = &vec[..]; | ||
let _ = &mut vec[..]; | ||
|
||
let ref_vec = &mut vec; | ||
let _ = &ref_vec[..]; | ||
let mut_slice = &mut ref_vec[..]; | ||
let _ = &mut mut_slice[..]; // Err, re-borrows slice | ||
|
||
let s = String::new(); | ||
let _ = &s[..]; | ||
|
||
static S: &[u8] = &[0, 1, 2]; | ||
let _ = &mut &S[..]; // Err, re-borrows slice | ||
|
||
let slice: &[u32] = &[0u32, 1u32]; | ||
let slice_ref = &slice; | ||
let _ = &slice_ref[..]; // Err, derefs slice | ||
|
||
let bytes: &[u8] = &[]; | ||
let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:9:13 | ||
| | ||
LL | let _ = &vec[..]; | ||
| ^^^^^^^^ help: dereference the original value instead: `&*vec` | ||
| | ||
= note: `-D clippy::deref-by-slicing` implied by `-D warnings` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:10:13 | ||
| | ||
LL | let _ = &mut vec[..]; | ||
| ^^^^^^^^^^^^ help: dereference the original value instead: `&mut *vec` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:13:13 | ||
| | ||
LL | let _ = &ref_vec[..]; | ||
| ^^^^^^^^^^^^ help: dereference the original value instead: `&**ref_vec` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:14:21 | ||
| | ||
LL | let mut_slice = &mut ref_vec[..]; | ||
| ^^^^^^^^^^^^^^^^ help: dereference the original value instead: `&mut **ref_vec` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:15:13 | ||
| | ||
LL | let _ = &mut mut_slice[..]; // Err, re-borrows slice | ||
| ^^^^^^^^^^^^^^^^^^ help: reborrow the original value instead: `&mut *mut_slice` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:18:13 | ||
| | ||
LL | let _ = &s[..]; | ||
| ^^^^^^ help: dereference the original value instead: `&*s` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:21:18 | ||
| | ||
LL | let _ = &mut &S[..]; // Err, re-borrows slice | ||
| ^^^^^^ help: reborrow the original value instead: `&*S` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:25:13 | ||
| | ||
LL | let _ = &slice_ref[..]; // Err, derefs slice | ||
| ^^^^^^^^^^^^^^ help: dereference the original value instead: `*slice_ref` | ||
|
||
error: slicing when dereferencing would work | ||
--> $DIR/deref_by_slicing.rs:28:13 | ||
| | ||
LL | let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows slice | ||
| ^^^^^^^^^^^^ help: reborrow the original value instead: `(&*bytes)` | ||
|
||
error: aborting due to 9 previous errors | ||
|
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,46 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused, clippy::deref_by_slicing)] | ||
#![warn(clippy::redundant_slicing)] | ||
|
||
use std::io::Read; | ||
|
||
fn main() { | ||
let slice: &[u32] = &[0]; | ||
let _ = slice; // Redundant slice | ||
|
||
let v = vec![0]; | ||
let _ = &v[..]; // Ok, results in `&[_]` | ||
let _ = (&*v); // Outer borrow is redundant | ||
|
||
static S: &[u8] = &[0, 1, 2]; | ||
let _ = &mut &S[..]; // Ok, re-borrows slice | ||
|
||
let mut vec = vec![0]; | ||
let mut_slice = &mut vec[..]; // Ok, results in `&mut [_]` | ||
let _ = &mut mut_slice[..]; // Ok, re-borrows slice | ||
|
||
let ref_vec = &vec; | ||
let _ = &ref_vec[..]; // Ok, results in `&[_]` | ||
|
||
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[..]; // Ok, derefs slice | ||
|
||
// Issue #7972 | ||
let bytes: &[u8] = &[]; | ||
let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap(); // Ok, re-borrows 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
Oops, something went wrong.