rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

New lint: prefer `addr_of` and `addr_of_mut`

Open Y-Nak opened this issue 5 years ago • 1 comments

What it does

Checks for the usage of &expr as *const T or &mut expr as *mut T, and suggest using ptr::addr_of or ptr::addr_of_mut instead. This would improve readability and avoid creating an unsafe reference that points to an uninitialized value or unaligned place. See also addr_of.

Categories (optional)

correctness or style

Drawbacks

None.

Example

let val = 1;
let p = &val as *const i32;

let mut val_mut = 1;
let p_mut = &mut val_mut as *mut i32;

Could be written as:

let val = 1;
let p = ptr::addr_of!(val);

let mut val_mut = 1;
let p_mut = ptr::addr_of_mut!(val_mut);

MSRV

1.51.0

Y-Nak avatar Mar 29 '21 03:03 Y-Nak

@rustbot claim

rmundy avatar Apr 06 '21 02:04 rmundy