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

update borrow_as_ptr to suggest &raw syntax #13689

Merged
merged 2 commits into from
Dec 5, 2024

Conversation

cyrgani
Copy link
Contributor

@cyrgani cyrgani commented Nov 14, 2024

This PR updates the borrow_as_ptr lint to no longer suggest addr_of! and addr_of_mut! and instead use the preferred &raw const and &raw mut syntax.

Not sure about two things:

  1. Do I need to set or update a MSRV for the lint anywhere?
  2. There is a borrow_as_ptr_no_std test as well as a borrow_as_ptr test. They used to be more relevant as the lint needed to select std or core, but that is gone now, so maybe the borrow_as_ptr_no_std should be deleted?

changelog: update borrow_as_ptr to suggest &raw syntax

@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 14, 2024
@y21
Copy link
Member

y21 commented Nov 15, 2024

Do I need to set or update a MSRV for the lint anywhere?

Only the suggestion needs it (we can still fall back to the slightly older addr_of! and lint if the MSRV is lower than 1.82). You can add an entry to the msrvs macro

1,82,0 { IS_NONE_OR, REPEAT_N }

and pass the Msrv instance to the check function and check with msrv.meets(&msrvs::RAW_REF_OP)

@y21
Copy link
Member

y21 commented Nov 15, 2024

Actually now looking more into it, I don't think this would ever do anything if gated behind an MSRV, because the lint borrow_as_ptr never runs for MSRV >= 1.76 as ref_as_ptr takes precedence in that case and suggests ptr::from_ref(&x)

@cyrgani
Copy link
Contributor Author

cyrgani commented Nov 15, 2024

You're right that it doesn't lint on current versions anymore, however, I fail to understand why this version restriction was added. For example, it should warn in this example and suggest &raw or addr_of_mut!, but doesn't (and ref_as_ptr doesn't warn either and shouldn't, since the idea of &raw is to avoid any intermediate references):

#![warn(clippy::pedantic)]
fn main() {
    let mut x = 2;
    let ptr1 = &mut x as *mut i32;
    let ptr2 = &mut x as *mut i32;
    //let ptr1 = &raw mut x;
    //let ptr2 = &raw mut x;
    unsafe {
        assert_eq!(*ptr1, *ptr2);
    }
}

@Jarcho
Copy link
Contributor

Jarcho commented Nov 27, 2024

That's nice and broken. borrow_as_ptr should take priority as it's a more specific lint.

@cyrgani can you make this two commits with the first flipping the order of borrow_as_ptr and ref_as_ptr. Make sure to have the commit message note that borrow_as_ptr is a more specific lint.

Like @y21 mentioned you'll need the MSRV check for the suggestion.

@Jarcho
Copy link
Contributor

Jarcho commented Nov 27, 2024

Note when flipping the order of the lints ref_as_ptr should only be run if no diagnostic was issued for borrow_as_ptr. You'll need to explicitly check if borrow_as_ptr is allowed via clippy_utils::is_lint_allowed and return whether span_lint was actually called.

@cyrgani cyrgani force-pushed the borrow_as_ptr_raw branch 2 times, most recently from 7d0b73a to 83241dd Compare November 28, 2024 10:33
Comment on lines 809 to 816
let was_ptr_from_ref_emitted = if self.msrv.meets(msrvs::PTR_FROM_REF) {
ref_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir)
} else {
false
};
if self.msrv.meets(msrvs::BORROW_AS_PTR) && !was_ptr_from_ref_emitted {
borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir, &self.msrv);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You have the right idea here, but borrow_as_pointer should be checked first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I've swapped the checks.

Copy link
Contributor

@Jarcho Jarcho 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 couple of small things remaining and this is good.

@@ -13,15 +14,12 @@ pub(super) fn check<'tcx>(
expr: &'tcx Expr<'_>,
cast_expr: &'tcx Expr<'_>,
cast_to: &'tcx Ty<'_>,
) {
msrv: &Msrv,
) -> bool {
if matches!(cast_to.kind, TyKind::Ptr(_))
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
&& let Some(std_or_core) = std_or_core(cx)
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be moved down to where it's actually needed.

clippy_lints/src/casts/borrow_as_ptr.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

Thank you.

@Jarcho Jarcho added this pull request to the merge queue Dec 5, 2024
Merged via the queue into rust-lang:master with commit a5e46a6 Dec 5, 2024
9 checks passed
@cyrgani cyrgani deleted the borrow_as_ptr_raw branch December 10, 2024 20:05
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.

4 participants