Skip to content

Commit c922bb9

Browse files
committed
fix ICE
1 parent cbdf17c commit c922bb9

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVe
5959
from_slice_ty, from_size, to_slice_ty, to_size,
6060
),
6161
|diag| {
62-
let cast_expr = match expr.kind {
62+
let cast_expr = match expr.peel_blocks().kind {
6363
ExprKind::Cast(cast_expr, ..) => cast_expr,
6464
_ => unreachable!("expr should be a cast as checked by expr_cast_chain_tys"),
6565
};

tests/ui/cast_slice_different_sizes.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,34 @@ fn main() {
3939
let long_chain_restore =
4040
r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8] as *const [u32];
4141
}
42+
43+
// foo and foo2 should not fire, they're the same size
44+
fn foo(x: *mut [u8]) -> *mut [u8] {
45+
x as *mut [u8]
46+
}
47+
48+
fn foo2(x: *mut [u8]) -> *mut [u8] {
49+
x as *mut _
50+
}
51+
52+
// Test that casts as part of function returns work
53+
fn bar(x: *mut [u16]) -> *mut [u8] {
54+
x as *mut [u8]
55+
}
56+
57+
fn uwu(x: *mut [u16]) -> *mut [u8] {
58+
x as *mut _
59+
}
60+
61+
fn bar2(x: *mut [u16]) -> *mut [u8] {
62+
x as _
63+
}
64+
65+
// function returns plus blocks
66+
fn blocks(x: *mut [u16]) -> *mut [u8] {
67+
({ x }) as _
68+
}
69+
70+
fn more_blocks(x: *mut [u16]) -> *mut [u8] {
71+
{ ({ x }) as _ }
72+
}

tests/ui/cast_slice_different_sizes.stderr

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,56 @@ error: casting between raw pointers to `[i32]` (element size 4) and `[u8]` (elem
4848
LL | let long_chain_loss = r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8];
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts`: `core::ptr::slice_from_raw_parts(r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const u8, ..)`
5050

51-
error: aborting due to 6 previous errors
51+
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
52+
--> $DIR/cast_slice_different_sizes.rs:53:36
53+
|
54+
LL | fn bar(x: *mut [u16]) -> *mut [u8] {
55+
| ____________________________________^
56+
LL | | x as *mut [u8]
57+
LL | | }
58+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut mut u8, ..)`
59+
60+
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
61+
--> $DIR/cast_slice_different_sizes.rs:57:36
62+
|
63+
LL | fn uwu(x: *mut [u16]) -> *mut [u8] {
64+
| ____________________________________^
65+
LL | | x as *mut _
66+
LL | | }
67+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut mut u8, ..)`
68+
69+
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
70+
--> $DIR/cast_slice_different_sizes.rs:61:37
71+
|
72+
LL | fn bar2(x: *mut [u16]) -> *mut [u8] {
73+
| _____________________________________^
74+
LL | | x as _
75+
LL | | }
76+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut mut u8, ..)`
77+
78+
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
79+
--> $DIR/cast_slice_different_sizes.rs:66:39
80+
|
81+
LL | fn blocks(x: *mut [u16]) -> *mut [u8] {
82+
| _______________________________________^
83+
LL | | ({ x }) as _
84+
LL | | }
85+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut mut u8, ..)`
86+
87+
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
88+
--> $DIR/cast_slice_different_sizes.rs:70:44
89+
|
90+
LL | fn more_blocks(x: *mut [u16]) -> *mut [u8] {
91+
| ____________________________________________^
92+
LL | | { ({ x }) as _ }
93+
LL | | }
94+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut mut u8, ..)`
95+
96+
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
97+
--> $DIR/cast_slice_different_sizes.rs:71:5
98+
|
99+
LL | { ({ x }) as _ }
100+
| ^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut mut u8, ..)`
101+
102+
error: aborting due to 12 previous errors
52103

0 commit comments

Comments
 (0)