Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::cast_slice_different_sizes: casting between raw pointers to `[i32]` (element size 4) and `[u8]` (element size 1) does not adjust the count
What it meansChecks for as casts between raw pointers to slices with differently sized elements. It is in the correctness group and denied by default (the build fails).
What you see
error: casting between raw pointers to `[i32]` (element size 4) and `[u8]` (element size 1) does not adjust the count --> main.rs:4:9 | 4 | let p = &a as *const [i32] as *const [u8]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts`: `core::ptr::slice_from_raw_parts(&a as *const [i32] as *const u8, ..)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#cast_slice_different_sizes = note: `#[deny(clippy::cast_slice_different_sizes)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
The produced raw pointer to a slice does not update its length metadata. The produced pointer will point to a different number of bytes than the original pointer because the length metadata of a raw slice pointer is in elements rather than bytes. Producing a slice reference from the raw pointer will either create a slice with less data (which can be surprising) or create a slice with more data and cause Undefined Behavior.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let a = [1_i32, 2, 3, 4];
let p = &a as *const [i32] as *const [u8];
unsafe {
println!("{:?}", &*p);
}
Write this instead
let a = [1_u8, 2, 3, 4];
let p = &a as *const [u8] as *const [u32];
unsafe {
println!("{:?}", &*p);
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::cast_slice_different_sizes)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
cast_slice_different_sizes = "allow"One run
cargo clippy -- -A clippy::cast-slice-different-sizesThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for as casts between raw pointers to slices with differently sized elements.
Why is this bad?
The produced raw pointer to a slice does not update its length metadata. The produced pointer will point to a different number of bytes than the original pointer because the length metadata of a raw slice pointer is in elements rather than bytes. Producing a slice reference from the raw pointer will either create a slice with less data (which can be surprising) or create a slice with more data and cause Undefined Behavior.
Example
// Missing data
let a = [1_i32, 2, 3, 4];
let p = &a as *const [i32] as *const [u8];
unsafe {
println!("{:?}", &*p);
}
// Undefined Behavior (note: also potential alignment issues)
let a = [1_u8, 2, 3, 4];
let p = &a as *const [u8] as *const [u32];
unsafe {
println!("{:?}", &*p);
}
Instead use ptr::slice_from_raw_parts to construct a slice from a data pointer and the correct length
let a = [1_i32, 2, 3, 4];
let old_ptr = &a as *const [i32];
// The data pointer is cast to a pointer to the target `u8` not `[u8]`
// The length comes from the known length of 4 i32s times the 4 bytes per i32
let new_ptr = core::ptr::slice_from_raw_parts(old_ptr as *const u8, 16);
unsafe {
println!("{:?}", &*new_ptr);
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- approx_constantapproximate value of `f{32, 64}::consts::PI` found
- async_yields_asyncan async construct yields a type which is itself awaitable
- bad_bit_maskChecks for incompatible bit masks in comparisons. The formula for detecting if an expression of the type _ <bit_op> m <cmp_op> c (where <bit_op> is one of {&, |} and <cmp_op> is one of {!=, >=, >, !=, >=, >}) can be determined from the following table: ComparisonBit OpExampleis alwaysFormula == or !=&x & 2 == 3falsec & m != c < or >=&x & 2 < 3truem < c > or <=&x & 1 > 1falsem <= c == or !=|x | 1 == 0falsec | m != c < or >=|x | 1 < 1falsem >= c <= or >|x | 1 > 0truem > c
- char_indices_as_byte_indicesChecks for usage of a character position yielded by .chars().enumerate() in a context where a byte index is expected, such as an argument to a specific str method or indexing into a str or String
- deprecated_semverthe since field must contain a semver-compliant version
- derive_ord_xor_partial_ordLints against manual PartialOrd and Ord implementations for types with a derived Ord or PartialOrd implementation
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::approx_constantapproximate value of `f{32, 64}::consts::PI` foundRead it next →Did this answer your question?