-
Couldn't load subscription status.
- Fork 1.8k
Closed
Closed
Copy link
Labels
A-lintArea: New lintsArea: New lints
Description
What it does
Code of the form
let x = &mut unsafe { *y };where the type of y is Copy successfully compiles, but very likely does not correspond to the intended behaviour.
Advantage
Avoid accidental copying of a value, which leads to the original object being unchanged.
Drawbacks
The code might be written as intended, especially if there are multiple statements inside the unsafe block.
Example
let mut x: u32 = 4; // Somewhere
// ...
let x_ptr: *mut u32 = &raw mut x;
let y = &mut unsafe { *x_ptr };Could be meant to be written as:
let y = unsafe { &mut *x_ptr };or
let y = unsafe { x_ptr.as_mut().unwrap() }or with nightly
let y = unsafe { x_ptr.as_mut_unchecked() }Comparison with existing lints
No response
Additional Context
The actual behaviour is in my opinion better written as
let y = unsafe { &mut (*x_ptr).clone() };or
let mut z = unsafe { *x_ptr };
let x = &mut z;to make the copy more explicit.
Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lints