about summary refs log tree commit diff
diff options
context:
space:
mode:
authorasquared31415 <34665709+asquared31415@users.noreply.github.com>2022-04-18 22:19:34 -0400
committerasquared31415 <34665709+asquared31415@users.noreply.github.com>2022-04-18 22:19:34 -0400
commitc922bb9443993df9f32ebf25bed76195cfab11f2 (patch)
tree6046450603b0f4bbfafb45a7c534216a457b5627
parentcbdf17c884116f17b1efe8c6dd3c41eb45d1342c (diff)
downloadrust-c922bb9443993df9f32ebf25bed76195cfab11f2.tar.gz
rust-c922bb9443993df9f32ebf25bed76195cfab11f2.zip
fix ICE
-rw-r--r--clippy_lints/src/casts/cast_slice_different_sizes.rs2
-rw-r--r--tests/ui/cast_slice_different_sizes.rs31
-rw-r--r--tests/ui/cast_slice_different_sizes.stderr53
3 files changed, 84 insertions, 2 deletions
diff --git a/clippy_lints/src/casts/cast_slice_different_sizes.rs b/clippy_lints/src/casts/cast_slice_different_sizes.rs
index 3608c1654d5..ff03f416d0b 100644
--- a/clippy_lints/src/casts/cast_slice_different_sizes.rs
+++ b/clippy_lints/src/casts/cast_slice_different_sizes.rs
@@ -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"),
                         };
diff --git a/tests/ui/cast_slice_different_sizes.rs b/tests/ui/cast_slice_different_sizes.rs
index 57270fcf52b..7ec137cc5b2 100644
--- a/tests/ui/cast_slice_different_sizes.rs
+++ b/tests/ui/cast_slice_different_sizes.rs
@@ -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 _ }
+}
diff --git a/tests/ui/cast_slice_different_sizes.stderr b/tests/ui/cast_slice_different_sizes.stderr
index 993e93c2bf3..0e018b62e60 100644
--- a/tests/ui/cast_slice_different_sizes.stderr
+++ b/tests/ui/cast_slice_different_sizes.stderr
@@ -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