Skip to content

Commit 505a44a

Browse files
committed
clean-up
1 parent c6f2557 commit 505a44a

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

clippy_lints/src/casts/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ declare_clippy_lint! {
228228
/// Dereferencing the resulting pointer may be undefined behavior.
229229
///
230230
/// ### Known problems
231-
/// Using [`std::ptr::read_unaligned`](https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html) and [`std::ptr::write_unaligned`](https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html) or
232-
/// similar on the resulting pointer is fine. Is over-zealous: casts with
233-
/// manual alignment checks or casts like `u64` -> `u8` -> `u16` can be
234-
/// fine. Miri is able to do a more in-depth analysis.
231+
/// Using [`std::ptr::read_unaligned`](https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html)
232+
/// and [`std::ptr::write_unaligned`](https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html)
233+
/// or similar on the resulting pointer is fine.
234+
///
235+
/// Is over-zealous: casts with manual alignment checks or casts like `u64` -> `u8` -> `u16` can be fine.
236+
/// Miri is able to do a more in-depth analysis.
235237
///
236238
/// ### Example
237239
/// ```no_run

tests/ui/cast_alignment.rs renamed to tests/ui/cast_ptr_alignment.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
//! Test casts for alignment issues
2-
31
#![feature(core_intrinsics)]
42
#![warn(clippy::cast_ptr_alignment)]
5-
#![allow(
6-
clippy::no_effect,
7-
clippy::unnecessary_operation,
8-
clippy::cast_lossless,
9-
clippy::borrow_as_ptr
10-
)]
3+
#![expect(clippy::no_effect, clippy::cast_lossless, clippy::borrow_as_ptr)]
114

125
fn main() {
136
/* These should be warned against */
@@ -33,13 +26,20 @@ fn main() {
3326
// cast to less-strictly-aligned type
3427
(&1u16 as *const u16) as *const u8;
3528
(&mut 1u16 as *mut u16) as *mut u8;
36-
// For c_void, we should trust the user. See #2677
29+
}
30+
31+
// For c_void, we should trust the user
32+
fn issue_2677() {
3733
(&1u32 as *const u32 as *const std::os::raw::c_void) as *const u32;
3834
(&1u32 as *const u32 as *const libc::c_void) as *const u32;
39-
// For ZST, we should trust the user. See #4256
35+
}
36+
37+
// For ZST, we should trust the user
38+
fn issue_4256() {
4039
(&1u32 as *const u32 as *const ()) as *const u32;
40+
}
4141

42-
// Issue #2881
42+
fn issue_2881() {
4343
let mut data = [0u8, 0u8];
4444
unsafe {
4545
let ptr = &data as *const [u8; 2] as *const u8;

tests/ui/cast_alignment.stderr renamed to tests/ui/cast_ptr_alignment.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
2-
--> tests/ui/cast_alignment.rs:16:5
2+
--> tests/ui/cast_ptr_alignment.rs:9:5
33
|
44
LL | (&1u8 as *const u8) as *const u16;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,19 +8,19 @@ LL | (&1u8 as *const u8) as *const u16;
88
= help: to override `-D warnings` add `#[allow(clippy::cast_ptr_alignment)]`
99

1010
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
11-
--> tests/ui/cast_alignment.rs:19:5
11+
--> tests/ui/cast_ptr_alignment.rs:12:5
1212
|
1313
LL | (&mut 1u8 as *mut u8) as *mut u16;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515

1616
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
17-
--> tests/ui/cast_alignment.rs:23:5
17+
--> tests/ui/cast_ptr_alignment.rs:16:5
1818
|
1919
LL | (&1u8 as *const u8).cast::<u16>();
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2121

2222
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
23-
--> tests/ui/cast_alignment.rs:26:5
23+
--> tests/ui/cast_ptr_alignment.rs:19:5
2424
|
2525
LL | (&mut 1u8 as *mut u8).cast::<u16>();
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)