-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Unnecessary references lint #138230
base: master
Are you sure you want to change the base?
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know little about lints' impls so 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
e113827
to
faf0620
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
@@ -894,6 +894,9 @@ lint_unnameable_test_items = cannot test inner items | |||
lint_unnecessary_qualification = unnecessary qualification | |||
.suggestion = remove the unnecessary path segments | |||
|
|||
lint_unnecessary_refs = creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers | |||
.suggestion = consider using `&raw const` for a safer and more explicit raw pointer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the suggestion also needs to have the right mutability
.suggestion = consider using `&raw const` for a safer and more explicit raw pointer | |
.suggestion = consider using `&raw {$mutbl}` for a safer and more explicit raw pointer |
&& let ExprKind::AddrOf(bk, mutbl, addr_of_exp) = exp.kind | ||
&& matches!(bk, BorrowKind::Ref) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason not to match directly?
&& let ExprKind::AddrOf(bk, mutbl, addr_of_exp) = exp.kind | |
&& matches!(bk, BorrowKind::Ref) | |
&& let ExprKind::AddrOf(BorrowKind::Ref, mutbl, addr_of_exp) = exp.kind |
&& let ExprKind::Unary(uo, _) = exp.kind | ||
&& matches!(uo, UnOp::Deref) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& let ExprKind::Unary(uo, _) = exp.kind | |
&& matches!(uo, UnOp::Deref) | |
&& let ExprKind::Unary(UnOp::Deref, _) = exp.kind |
@@ -0,0 +1,31 @@ | |||
//@ run-rustfix | |||
|
|||
#![deny(unnecessary_refs)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer that we do not override the lint level
#![deny(unnecessary_refs)] |
and use //@ check-pass
/WARN
&& let ExprKind::Field(exp, _) = addr_of_exp.kind | ||
&& let ExprKind::Unary(uo, _) = exp.kind | ||
&& matches!(uo, UnOp::Deref) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the reason for limiting the lint to expressions with a field access? the reasoning also applies to field-less access like &val as *const i32
no?
we should IMO just lint on every operation like this &<expr> as *<const/mut> <type>
&& let ExprKind::Field(exp, _) = addr_of_exp.kind | |
&& let ExprKind::Unary(uo, _) = exp.kind | |
&& matches!(uo, UnOp::Deref) |
Close #127724