Skip to content

New lint: prefer addr_of and addr_of_mut #6995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Y-Nak opened this issue Mar 29, 2021 · 1 comment · Fixed by #8210
Closed

New lint: prefer addr_of and addr_of_mut #6995

Y-Nak opened this issue Mar 29, 2021 · 1 comment · Fixed by #8210
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@Y-Nak
Copy link
Contributor

Y-Nak commented Mar 29, 2021

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 Y-Nak added the A-lint Area: New lints label Mar 29, 2021
@Y-Nak Y-Nak changed the title New lint: forbid addr_of and addr_mut_of New lint: forbid addr_of and addr_of_mut Mar 29, 2021
@Y-Nak Y-Nak changed the title New lint: forbid addr_of and addr_of_mut New lint: prefer addr_of and addr_of_mut Mar 29, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Mar 29, 2021
@rmundy
Copy link

rmundy commented Apr 6, 2021

@rustbot claim

bors added a commit that referenced this issue Jan 11, 2022
Add borrow_as_ptr lint

Closes: #6995

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

---

changelog: new lint: [`borrow_as_ptr`]
@bors bors closed this as completed in 3298de7 Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants