Skip to content

Add lint converting reference to pointer cast into ptr::from_{ref,mut} method calls #10130

New issue

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

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

Already on GitHub? # to your account

Closed
SUPERCILEX opened this issue Dec 30, 2022 · 0 comments · Fixed by #12087
Closed
Labels
A-lint Area: New lints

Comments

@SUPERCILEX
Copy link

SUPERCILEX commented Dec 30, 2022

What it does

&something as *const Something would be converted to ptr::from_ref(&something)

See the tracking issue: rust-lang/rust#106116

Once those methods are stabilized, we should be able to re-open #9802 and finally ban all as casts for pointers.

Lint Name

as_ptr_cast

Category

style, pedantic

@SUPERCILEX SUPERCILEX added the A-lint Area: New lints label Dec 30, 2022
bors added a commit that referenced this issue Feb 4, 2024
Add new lint: `ref_as_ptr`

Fixes #10130

Added new lint `ref_as_ptr` that checks for conversions from references to pointers and suggests using `std::ptr::from_{ref, mut}` instead.

The name is different than suggested in the issue (`as_ptr_cast`) since there were some other lints with similar names (`ptr_as_ptr`, `borrow_as_ptr`) and I wanted to follow the convention.

Note that this lint conflicts with the `borrow_as_ptr` lint in the sense that it recommends changing `&foo as *const _` to `std::ptr::from_ref(&foo)` instead of `std::ptr::addr_of!(foo)`. Personally, I think the former is more readable and, in contrast to `addr_of` macro, can be also applied to temporaries (cf. #9884).

---

changelog: New lint: [`ref_as_ptr`]
[#12087](#12087)
bors added a commit that referenced this issue Feb 4, 2024
Add new lint: `ref_as_ptr`

Fixes #10130

Added new lint `ref_as_ptr` that checks for conversions from references to pointers and suggests using `std::ptr::from_{ref, mut}` instead.

The name is different than suggested in the issue (`as_ptr_cast`) since there were some other lints with similar names (`ptr_as_ptr`, `borrow_as_ptr`) and I wanted to follow the convention.

Note that this lint conflicts with the `borrow_as_ptr` lint in the sense that it recommends changing `&foo as *const _` to `std::ptr::from_ref(&foo)` instead of `std::ptr::addr_of!(foo)`. Personally, I think the former is more readable and, in contrast to `addr_of` macro, can be also applied to temporaries (cf. #9884).

---

changelog: New lint: [`ref_as_ptr`]
[#12087](#12087)
bors added a commit that referenced this issue Feb 4, 2024
Add new lint: `ref_as_ptr`

Fixes #10130

Added new lint `ref_as_ptr` that checks for conversions from references to pointers and suggests using `std::ptr::from_{ref, mut}` instead.

The name is different than suggested in the issue (`as_ptr_cast`) since there were some other lints with similar names (`ptr_as_ptr`, `borrow_as_ptr`) and I wanted to follow the convention.

Note that this lint conflicts with the `borrow_as_ptr` lint in the sense that it recommends changing `&foo as *const _` to `std::ptr::from_ref(&foo)` instead of `std::ptr::addr_of!(foo)`. Personally, I think the former is more readable and, in contrast to `addr_of` macro, can be also applied to temporaries (cf. #9884).

---

changelog: New lint: [`ref_as_ptr`]
[#12087](#12087)
@bors bors closed this as completed in 34e4c9f Feb 4, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant