rust/tests
bors 0c483f69db Auto merge of #8445 - asquared31415:slice_ptr_cast, r=llogiq
Llint for casting between raw slice pointers with different element sizes

This lint disallows using `as` to convert from a raw pointer to a slice (e.g. `*const [i32]`, `*mut [Foo]`) to any other raw pointer to a slice if the element types have different sizes.  When a raw slice pointer is cast, the data pointer and count metadata are preserved.  This means that when the size of the inner slice's element type changes, the total number of bytes pointed to by the count changes.  For example a `*const [i32]` with length 4 (four `i32` elements) is cast `as *const [u8]` the resulting pointer points to four `u8` elements at the same address, losing most of the data.  When the size *increases* the resulting pointer will point to *more* data, and accessing that data will be UB.

On its own, *producing* the pointer isn't actually a problem, but because any use of the pointer as a slice will either produce surprising behavior or cause UB I believe this is a correctness lint.  If the pointer is not intended to be used as a slice, the user should instead use any of a number of methods to produce just a data pointer including an `as` cast to a thin pointer (e.g. `p as *const i32`) or if the pointer is being created from a slice, the `as_ptr` method on slices.  Detecting the intended use of the pointer is outside the scope of this lint, but I believe this lint will also lead users to realize that a slice pointer is only for slices.

There is an exception to this lint when either of the slice element types are zero sized (e.g `*mut [()]`).  The total number of bytes pointed to by the slice with a zero sized element is zero.  In that case preserving the length metadata is likely intended as a workaround to get the length metadata of a slice pointer though a zero sized slice.

The lint does not forbid casting pointers to slices with the *same* element size as the cast was likely intended to reinterpret the data in the slice as some equivalently sized data and the resulting pointer will behave as intended.

---

changelog: Added ``[`cast_slice_different_sizes`]``, a lint that disallows using `as`-casts to convert between raw pointers to slices when the elements have different sizes.
2022-03-06 07:46:56 +00:00
..
test_utils
ui Auto merge of #8445 - asquared31415:slice_ptr_cast, r=llogiq 2022-03-06 07:46:56 +00:00
ui-cargo ignore test on windows since I don't know why compiletest does not hand paths properly there 2022-02-06 17:21:32 +01:00
ui-internal Implement internal lint for MSRV lints 2022-03-01 09:40:07 +00:00
ui-toml Merge commit 'a98e7ab8b94485be6bd03e0c6b8682ecab5b52e6' into clippyup 2022-01-27 15:12:45 +01:00
workspace_test
clippy.toml
compile-test.rs tests: use std:🧵:available_parallelism() instead of num_cpus to get thread count 2022-03-01 21:32:18 +01:00
dogfood.rs
fmt.rs
integration.rs
lint_message_convention.rs
missing-test-files.rs
versioncheck.rs
workspace.rs