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:
bors 2024-08-03 07:23:58 +00:00
commit 1aa686b9c0
4 changed files with 20 additions and 3 deletions

View file

@ -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")

View file

@ -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;
}

View file

@ -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[..];
}

View file

@ -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