Skip to content

Commit 58027e2

Browse files
committed
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
1 parent b31bce4 commit 58027e2

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

clippy_lints/src/redundant_slicing.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
113113
a.kind,
114114
Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. }))
115115
)
116-
}) {
117-
// The slice was used to make a temporary reference.
116+
}) || (matches!(
117+
cx.typeck_results().expr_ty(indexed).ref_mutability(),
118+
Some(Mutability::Mut)
119+
) && mutability == Mutability::Not)
120+
{
118121
(DEREF_BY_SLICING_LINT, "&*", "reborrow the original value instead")
119122
} else if deref_count != 0 {
120123
(DEREF_BY_SLICING_LINT, "", "dereference the original value instead")

tests/ui/deref_by_slicing.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ fn main() {
2525

2626
let bytes: &[u8] = &[];
2727
let _ = (&*bytes).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows slice
28+
29+
// issue 12751
30+
let a = &mut [1, 2, 3][..];
31+
let _ = &*a;
2832
}

tests/ui/deref_by_slicing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ fn main() {
2525

2626
let bytes: &[u8] = &[];
2727
let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows slice
28+
29+
// issue 12751
30+
let a = &mut [1, 2, 3][..];
31+
let _ = &a[..];
2832
}

tests/ui/deref_by_slicing.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ error: slicing when dereferencing would work
5555
LL | let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap(); // Err, re-borrows slice
5656
| ^^^^^^^^^^^^ help: reborrow the original value instead: `(&*bytes)`
5757

58-
error: aborting due to 9 previous errors
58+
error: slicing when dereferencing would work
59+
--> tests/ui/deref_by_slicing.rs:31:13
60+
|
61+
LL | let _ = &a[..];
62+
| ^^^^^^ help: reborrow the original value instead: `&*a`
63+
64+
error: aborting due to 10 previous errors
5965

0 commit comments

Comments
 (0)