This commit is contained in:
asquared31415 2022-04-18 22:19:34 -04:00
parent cbdf17c884
commit c922bb9443
3 changed files with 84 additions and 2 deletions

View file

@ -59,7 +59,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVe
from_slice_ty, from_size, to_slice_ty, to_size,
),
|diag| {
let cast_expr = match expr.kind {
let cast_expr = match expr.peel_blocks().kind {
ExprKind::Cast(cast_expr, ..) => cast_expr,
_ => unreachable!("expr should be a cast as checked by expr_cast_chain_tys"),
};

View file

@ -39,3 +39,34 @@ fn main() {
let long_chain_restore =
r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8] as *const [u32];
}
// foo and foo2 should not fire, they're the same size
fn foo(x: *mut [u8]) -> *mut [u8] {
x as *mut [u8]
}
fn foo2(x: *mut [u8]) -> *mut [u8] {
x as *mut _
}
// Test that casts as part of function returns work
fn bar(x: *mut [u16]) -> *mut [u8] {
x as *mut [u8]
}
fn uwu(x: *mut [u16]) -> *mut [u8] {
x as *mut _
}
fn bar2(x: *mut [u16]) -> *mut [u8] {
x as _
}
// function returns plus blocks
fn blocks(x: *mut [u16]) -> *mut [u8] {
({ x }) as _
}
fn more_blocks(x: *mut [u16]) -> *mut [u8] {
{ ({ x }) as _ }
}

View file

@ -48,5 +48,56 @@ error: casting between raw pointers to `[i32]` (element size 4) and `[u8]` (elem
LL | let long_chain_loss = r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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, ..)`
error: aborting due to 6 previous errors
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
--> $DIR/cast_slice_different_sizes.rs:53:36
|
LL | fn bar(x: *mut [u16]) -> *mut [u8] {
| ____________________________________^
LL | | x as *mut [u8]
LL | | }
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut mut u8, ..)`
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
--> $DIR/cast_slice_different_sizes.rs:57:36
|
LL | fn uwu(x: *mut [u16]) -> *mut [u8] {
| ____________________________________^
LL | | x as *mut _
LL | | }
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut mut u8, ..)`
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
--> $DIR/cast_slice_different_sizes.rs:61:37
|
LL | fn bar2(x: *mut [u16]) -> *mut [u8] {
| _____________________________________^
LL | | x as _
LL | | }
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut mut u8, ..)`
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
--> $DIR/cast_slice_different_sizes.rs:66:39
|
LL | fn blocks(x: *mut [u16]) -> *mut [u8] {
| _______________________________________^
LL | | ({ x }) as _
LL | | }
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut mut u8, ..)`
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
--> $DIR/cast_slice_different_sizes.rs:70:44
|
LL | fn more_blocks(x: *mut [u16]) -> *mut [u8] {
| ____________________________________________^
LL | | { ({ x }) as _ }
LL | | }
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut mut u8, ..)`
error: casting between raw pointers to `[mut u16]` (element size 2) and `[mut u8]` (element size 1) does not adjust the count
--> $DIR/cast_slice_different_sizes.rs:71:5
|
LL | { ({ x }) as _ }
| ^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut mut u8, ..)`
error: aborting due to 12 previous errors