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
|
@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 comment has been minimized.
This comment has been minimized.
|
I'm afraid I don't have the capacity to do code-level review here. |
This comment has been minimized.
This comment has been minimized.
abc1e1e to
f4e1186
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. |
f4e1186 to
b6eda97
Compare
Something like |
|
How about I realise that this drops the fact that the current checks only for creating one through a decayed reference, but perhaps there are future variants of the lint (especially thinking of array slicing) that check for other intermediates with the same purpose. |
| /// This lint is "allow" by default because promoting it would require a | ||
| /// large amount of churn across the standard library to remove the many | ||
| /// unnecessary references it currently creates. |
There was a problem hiding this comment.
Surely the code of the standard library itself is the least relevant thing when considering churn, since the lint could be #![allow]ed there (as a much smaller change than this PR, even)? Shouldn’t it say something more like (from unreachable-pub):
| /// This lint is "allow" by default because promoting it would require a | |
| /// large amount of churn across the standard library to remove the many | |
| /// unnecessary references it currently creates. | |
| /// This lint is "allow" by default because it will trigger for a large | |
| /// amount of existing Rust code. | |
| /// Eventually it is desired for this to become warn-by-default. |
|
|
||
| #[derive(Diagnostic)] | ||
| #[diag( | ||
| "creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers" |
There was a problem hiding this comment.
This is mixing singular and plural.
| "creating an intermediate reference implies aliasing requirements even when immediately casting to raw pointers" | |
| "creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointer" |
| /// #![warn(unnecessary_refs)] | ||
| /// | ||
| /// fn via_ref(x: *const (i32, i32)) -> *const i32 { | ||
| /// unsafe { &(*x).0 as *const i32 } |
There was a problem hiding this comment.
References can coerce to raw pointers, so this example could be simplified:
| /// unsafe { &(*x).0 as *const i32 } | |
| /// unsafe { &(*x).0 } |
I think this is beneficial because it helps show just how tiny the piece of code that undesirably introduces a reference can be.
Also, I notice that this function as written is not just dubious but unsound: it is UB if x is null, unaligned, aliased with &mut, or not readable. Most examples of allow-by-default lints are going to be much more “acceptable but not ideal” code, so it might be worth calling out this one in particular as unsound. (On the other hand, that’s a distraction from the typical case where this lint applies.)
There was a problem hiding this comment.
Yeah would be better to find an example where the raw pointer version is actually correct.
| /// less explicit and can lead to undefined behavior. Creating a reference | ||
| /// induces aliasing assumptions that the compiler relies on, so an | ||
| /// otherwise-pointless reference can cause undefined behavior; it is also | ||
| /// UB if the reference is unaligned. Avoiding them keeps the code more |
There was a problem hiding this comment.
This description seems to me to be in an inconsistent middle ground of explaining reference validity: it is mentioning the requirement of alignment, but not others such as being non-null. I think it should focus on the aliasing constraint alone (with a link to the Reference for other validity rules), because most code that runs but has unobvious problems (and thus needs the attention of this lint) is going to be using an aligned, readable, non-null pointer, but neglecting aliasing.
|
For a name, how about |
Works for me, in the plural (per RFC 0344): (And thanks to @RalfJung and @WorldSEnder for the other good suggestions above.) |
|
Should probably be |
+1. |
View all comments
Close #127724