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
64132e5 to
b0d836f
Compare
This comment has been minimized.
This comment has been minimized.
4acb822 to
d6d72c8
Compare
This comment has been minimized.
This comment has been minimized.
|
☔ The latest upstream changes (presumably #139012) made this pull request unmergeable. Please resolve the merge conflicts. |
|
My PR is currently blocked because the |
|
@obeis What's the status of this PR? Was the |
|
@Urgau I think |
|
It looks like most of this PR is about fixing occurrences of the lint in Maybe it'd make sense to land this lint as allow-by-default first, and then only slowly enable it throughout the library by actually removing the unnecessary references? Given the amount of times this triggers on the library, it seems likely to also trigger a lot in the wider ecosystem, so some sort of slow staging starting with allow-by-default might also make sense from that perspective. (We should definitely get t-lang input before landing this.) |
d6d72c8 to
10c6d3c
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. |
|
@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: No concerns currently listed. 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. |
|
|
||
| #[derive(Diagnostic)] | ||
| #[diag( | ||
| "creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers" |
There was a problem hiding this comment.
| "creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers" | |
| "creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers" |
View all comments
Close #127724