mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Auto merge of #13126 - apoisternex:issue12751, r=dswij
Fix [`redundant_slicing`] when the slice is behind a mutable reference Fixes #12751 changelog: Fix [`redundant_slicing`] when the slice is behind a mutable reference and a immutable reference is expected.
This commit is contained in:
commit
1aa686b9c0
4 changed files with 20 additions and 3 deletions
|
@ -113,8 +113,11 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
|
|||
a.kind,
|
||||
Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. }))
|
||||
)
|
||||
}) {
|
||||
// The slice was used to make a temporary reference.
|
||||
}) || (matches!(
|
||||
cx.typeck_results().expr_ty(indexed).ref_mutability(),
|
||||
Some(Mutability::Mut)
|
||||
) && mutability == Mutability::Not)
|
||||
{
|
||||
(DEREF_BY_SLICING_LINT, "&*", "reborrow the original value instead")
|
||||
} else if deref_count != 0 {
|
||||
(DEREF_BY_SLICING_LINT, "", "dereference the original value instead")
|
||||
|
|
|
@ -25,4 +25,8 @@ fn main() {
|
|||
|
||||
let bytes: &[u8] = &[];
|
||||
let _ = (&*bytes).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows slice
|
||||
|
||||
// issue 12751
|
||||
let a = &mut [1, 2, 3][..];
|
||||
let _ = &*a;
|
||||
}
|
||||
|
|
|
@ -25,4 +25,8 @@ fn main() {
|
|||
|
||||
let bytes: &[u8] = &[];
|
||||
let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows slice
|
||||
|
||||
// issue 12751
|
||||
let a = &mut [1, 2, 3][..];
|
||||
let _ = &a[..];
|
||||
}
|
||||
|
|
|
@ -55,5 +55,11 @@ error: slicing when dereferencing would work
|
|||
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
|
||||
error: slicing when dereferencing would work
|
||||
--> tests/ui/deref_by_slicing.rs:31:13
|
||||
|
|
||||
LL | let _ = &a[..];
|
||||
| ^^^^^^ help: reborrow the original value instead: `&*a`
|
||||
|
||||
error: aborting due to 10 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue