Skip to content

New lint: prefer addr_of and addr_of_mut #6995

Closed
@Y-Nak

Description

@Y-Nak

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

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions