Skip to content

Commit

Permalink
Auto merge of rust-lang#13126 - apoisternex:issue12751, r=dswij
Browse files Browse the repository at this point in the history
Fix [`redundant_slicing`] when the slice is behind a mutable reference

Fixes rust-lang#12751

changelog: Fix [`redundant_slicing`] when the slice is behind a mutable reference and a immutable reference is expected.
  • Loading branch information
bors committed Aug 3, 2024
2 parents 2fc74a3 + 58027e2 commit 1aa686b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 5 additions & 2 deletions clippy_lints/src/redundant_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/deref_by_slicing.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions tests/ui/deref_by_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..];
}
8 changes: 7 additions & 1 deletion tests/ui/deref_by_slicing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1aa686b

Please sign in to comment.