Unnecessary references lint#138230
Conversation
|
r? @Noratrieb rustbot has assigned @Noratrieb. Use |
|
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. |
This comment has been minimized.
This comment has been minimized.
compiler-errors
left a comment
There was a problem hiding this comment.
This lint seems really specific to a single kind of expression, and there are plenty of other cases where unnecessary references are created when the user is trying to create a raw pointer. Unless this can be greatly generalized, it doesn't really seem worth adding this.
|
r? RalfJung |
|
I agree, and my intention is to generalize this lint to cover all unnecessarily created references. Could you please list other cases that you think should be included? I can update this PR to cover them or create an issue to track these cases and mention that they should be added to the |
|
Sorry, I am swamped. Happy to discuss which cases the lint shoild fire on, but I can't review the implementation.
r? compiler
|
|
Do we have some people who are our "linting experts"? |
Which examples did you have in mind? A starting point might be to uplift https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr from cliippy to rustc, as you mention. It is not clear to me what the difference is between that lint and this one. |
cc @Urgau |
|
Happy to take over the review. If @Nadrieril doesn't want to review it of course. As for the lint it-self, I join @RalfJung that this is lint is currently As a drive-by, |
|
Much appreciated :) r? @Urgau |
1e686f1 to
e113827
Compare
|
@RalfJung I agree, let's land this as allow-by-default for now. |
This comment has been minimized.
This comment has been minimized.
10c6d3c to
5f1cb56
Compare
|
Makes sense to me. Thanks @obeis and @RalfJung. Lint detailsThis is an allow-by-default lint:
This lint is proposed at allow-by-default so it can land while we adjust the standard library and gauge effects on the ecosystem. @rfcbot fcp merge lang In terms of the current implementation, I understand the following to be true. fn redundant_block(x: u8) {
let _x = { &x } as *const u8;
}It doesn't fire for the above, but should. fn vec_index_deref(x: *mut Vec<u8>) -> *mut u8 {
unsafe { &mut (*x)[0] as *mut u8 }
}It fires for this one, but should not, because the place isn't reachable through built-in projections and so a reference is materialized in MIR regardless. After applying the lint's suggestion, the MIR is the same, and the fn vec_index(v: &mut Vec<u8>) -> *mut u8 {
&mut v[0] as *mut u8
}Similar to the above, the lint fires and applying the suggestion results in code that produces the same MIR. (In this case, though, |
|
@traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
I'm having a little bit of difficulty following the conversation and understanding what the current implementation does. I can't find a consolidated description. Does the current implementation only check for Is there an intention for this lint to cover any situation where a borrow is "unnecessary"? For example, would it also encompass
|
|
Replacing
This sounds like
Not from my side, no. It is specifically about borrows that are unnecessary where removing them actually can make a semantic difference (which I think can only happen if removing the borrow entirely avoids creating any reference, using only raw pointers instead). |
|
@rfcbot concern name-and-scope @ehuss asked:
@RalfJung's reply was:
If we mean to limit the scope of the lint in this way, probably the current name is a bit too encompassing. Do we have any other ideas for that? As @ehuss notes, it'd be good to include these intended scope restrictions in the lint documentation as well. |
5f1cb56 to
abc1e1e
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
I'm afraid I don't have the capacity to do code-level review here. |
|
☔ The latest upstream changes (presumably #157575) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
| if let ExprKind::Cast(exp, ty) = expr.kind | ||
| && let ExprKind::AddrOf(BorrowKind::Ref, mutbl, addr_of_exp) = exp.kind | ||
| && let TyKind::Ptr(_) = ty.kind |
There was a problem hiding this comment.
Unless I'm missing something, this check doesn't take into account temporaries, which contrary to & don't work with &raw const.
error[E0745]: cannot take address of a temporary
--> src/main.rs:3:24
|
3 | let a = &raw const 1;
| ^ temporary value
There was a problem hiding this comment.
Could you add more tests with temporaries, multiple casts, inner blocks, ...
View all comments
Close #127724