Skip to content
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

Add new lint: ref_as_ptr #12087

Merged
merged 1 commit into from
Feb 4, 2024
Merged

Conversation

marcin-serwin
Copy link
Contributor

@marcin-serwin marcin-serwin commented Jan 3, 2024

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

@rustbot
Copy link
Collaborator

rustbot commented Jan 3, 2024

r? @xFrednet

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 3, 2024
@marcin-serwin marcin-serwin changed the title Ref as ptr cast Add new lint: ref_as_ptr Jan 3, 2024
@asquared31415
Copy link
Contributor

In my opinion borrow_as_ptr isn't particularly useful anyway, I think that this lint is more useful and has better readability. The docs on borrow_as_ptr are also misleading because the code that it lints on can't cause the issues that it claims to prevent.

@xFrednet
Copy link
Member

I'm currently busy with exam season. Queen @blyxyas, would you do me the honer and review this PR? :D

r? @blyxyas

@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 2024

Could not assign reviewer from: blyxyas.
User(s) blyxyas are either the PR author, already assigned, or on vacation, and there are no other candidates.
Use r? to specify someone else to assign.

@xFrednet xFrednet assigned blyxyas and unassigned xFrednet Jan 11, 2024
@blyxyas
Copy link
Member

blyxyas commented Jan 11, 2024

?? I'll check triagebot.toml, but AFAIK I'm still in the reviewer rotation and not on vacation (maybe a bors bug?)

@xFrednet
Copy link
Member

Yeah IDK, was weird, does this work?

r? @xFrednet

@rustbot rustbot assigned xFrednet and unassigned blyxyas Jan 11, 2024
@xFrednet
Copy link
Member

😂

r? @blyxyas

@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 2024

Could not assign reviewer from: blyxyas.
User(s) blyxyas are either the PR author, already assigned, or on vacation, and there are no other candidates.
Use r? to specify someone else to assign.

@xFrednet
Copy link
Member

Lol, maybe it hasn't reloaded the configuration yet?

@blyxyas
Copy link
Member

blyxyas commented Jan 11, 2024

Philipp reverted my change unassigning me as on-vacation. (on a subtree sync, idk why or how)
I'll make another PR (=^w^=)

@xFrednet xFrednet assigned blyxyas and unassigned xFrednet Jan 11, 2024
@blyxyas
Copy link
Member

blyxyas commented Jan 13, 2024

Hmmm... How is this different from borrow_as_ptr? The example is the same for both lints

@marcin-serwin
Copy link
Contributor Author

marcin-serwin commented Jan 13, 2024

The borrow_as_ptr is recommending the macro addr_of instead of the function from_ref (both in std::ptr).

@blyxyas
Copy link
Member

blyxyas commented Jan 14, 2024

Hmmm 🤔 I'm not really seeing the benefits of from_ref or addr_of over each other.
I think I'll nominate this, and let's see what the team thinks of this conundrum =^w^=

@blyxyas blyxyas added the I-nominated Issue: Nominated to be discussed at the next Clippy meeting label Jan 14, 2024
@asquared31415
Copy link
Contributor

from_ref directly turns a reference into its pointer, it's sugar for x as *const T where x: &T.

addr_of! gets the address of a "place" which can be many different things, such as the address of a local (addr_of!(my_owned_local)), it can go through field projections (addr_of!(my_struct.field)), and it interacts with pointer dereferences to prevent making an intermediate reference.

They are useful in slightly different situations, specifically addr_of! can technically express more, but because it can do so many things, it's more cognitive load to do addr_of!(*x) to turn a &T into a *const T than it is to do ptr::from_ref(x).

@blyxyas blyxyas removed the I-nominated Issue: Nominated to be discussed at the next Clippy meeting label Jan 25, 2024
@blyxyas
Copy link
Member

blyxyas commented Jan 25, 2024

Okis, so the conclusions from our improvised meeting is that both lints can coexist. I'm still not to fond of this (e.g. what if someone used #[warn(clippy::all)] and --fix, or simply manually enabled them both?) but I'll review this because that's what the council decided

Meow meow, sorry for the long delay 🙏, I'll review it (at least start to do it) today

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good first iteration! Following Philipp's suggestion, and because I think that this new lint is more useful than borrow_as_ptr (but they lint in identical situations), could you move that lint into restriction?

I don't remember if there's a formal process to move a lint's category (⁎˃ᆺ˂), but I've asked about it. If there isn't a necessary process we didn't do, I'll merge this.

@marcin-serwin
Copy link
Contributor Author

marcin-serwin commented Jan 26, 2024

I've changed the category of borrow_as_ptr though I have mixed feelings about that. For one thing, it's not a restriction: from the book: 'This group contains lints that "restrict" the language in some way.' and I don't think it's doing that, since it recommends using addr_of! instead of restricting as casts generally. Secondly, the from_ref functions aren't even stable yet, so we are effectively disabling linting &x as *const _ for someone using Rust before 1.76.0 unless they turn on this lint specifically.

@marcin-serwin marcin-serwin requested a review from blyxyas January 26, 2024 15:42
@blyxyas
Copy link
Member

blyxyas commented Jan 29, 2024

Hmmm... you're right. We'll try with the second solution.
Make the check function return a boolean, and only if this check returns false (because the lint didn't lint, possibly because of MSRV), we'll then check for borrow_as_ptr.

This way, both lints can be suspicious, and we don't have to prioritize one over the other.

@marcin-serwin
Copy link
Contributor Author

marcin-serwin commented Feb 1, 2024

I've moved the msrv check higher instead of returning bool from the check function. I think it's equivalent since the ref_as_ptr should kick every time the borrow_as_ptr does (or at least that's my intention) and it's easier to read.

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little bit of polish. After this little change, could you squash the commits?

cx,
REF_AS_PTR,
expr.span,
"ref as raw pointer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ref as raw pointer",
"reference as raw pointer",

@blyxyas
Copy link
Member

blyxyas commented Feb 4, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Feb 4, 2024

📌 Commit 73f3fce has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 4, 2024

⌛ Testing commit 73f3fce with merge b151aa5...

bors added a commit that referenced this pull request 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
Copy link
Contributor

bors commented Feb 4, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Feb 4, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Feb 4, 2024

📌 Commit a3baebc has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 4, 2024

⌛ Testing commit a3baebc with merge 69f2dc0...

bors added a commit that referenced this pull request 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
Copy link
Contributor

bors commented Feb 4, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Feb 4, 2024

@bors retry

@bors
Copy link
Contributor

bors commented Feb 4, 2024

⌛ Testing commit a3baebc with merge 7066984...

bors added a commit that referenced this pull request 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
Copy link
Contributor

bors commented Feb 4, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Feb 4, 2024

@bors retry

@bors
Copy link
Contributor

bors commented Feb 4, 2024

⌛ Testing commit a3baebc with merge 34e4c9f...

@bors
Copy link
Contributor

bors commented Feb 4, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas
Pushing 34e4c9f to master...

@bors bors merged commit 34e4c9f into rust-lang:master Feb 4, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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