-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Enhance suggestions of dropping_*
lints
#126275
Conversation
Dropping mutable references is not a no-op, it makes the mutable reference inaccessible (as it is not a `Copy` type).
Previously, the lint suggested to change code like `drop(&variable);` into `let _ = &variable;`. Now it suggests to remove the whole expression.
The lint will now suggest to simply remove the `drop` call instead of changing it to `let _ = …`.
r? @nnethercote rustbot has assigned @nnethercote. Use |
T-lang was the one to suggest that we should explicitly suggest |
I have read through #125972, and now this comment:
I am now quite confused! There has been a lot of back and forth, it's not clear to me what the conclusion was and if there is consensus about that conclusion. Can someone summarize for me the individual changes being proposed here, each with a simple example? Thanks. |
The first commit tweaks the wording of the lint if a mutable reference is The second commit suggests to remove the entire The third commit adds some tests for the latter thing. |
@@ -45,7 +45,7 @@ LL - drop(&&owned1); | |||
LL + let _ = &&owned1; | |||
| | |||
|
|||
error: calls to `std::mem::drop` with a reference instead of an owned value does nothing |
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.
So it's the "does nothing" part that is the problem here? I.e. it's not true for mutable references?
How about just changing the message to this: "a call to std::mem::drop
that is passed a reference drops the reference, not the underlying value". It is true for both kinds of reference, and avoids the need for two different error messages?
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.
So it's the "does nothing" part that is the problem here? I.e. it's not true for mutable references?
Yes.
How about just changing the message to this: "a call to
std::mem::drop
that is passed a reference drops the reference, not the underlying value". It is true for both kinds of reference, and avoids the need for two different error messages?
Works for me, I'm not attached to the original message.
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.
So it's the "does nothing" part that is the problem here? I.e. it's not true for mutable references?
Yes.
IMO, this isn't true, the call to std::mem::drop
with a reference (mutable or not) does nothing to the reference or the underline value. The fact that is does something to the binding/variable it-self is unfortunate but IMO irrelevant here.
How about just changing the message to this: "a call to
std::mem::drop
that is passed a reference drops the reference, not the underlying value".
I would personally prefer: "calls to std::mem::drop
with a reference does not drop the underline value"
| | ||
LL - drop(&owned1); | ||
LL + let _ = &owned1; |
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.
Is this the best suggestion? Perhaps changing it to drop(owned1)
is what the author really intended?
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 agree that suggesting let _ = ...
is a bit silly when the drop
argument has an explicit &
. It's more reasonable for something like drop(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.
I agree that suggesting
let _ = ...
is a bit silly when thedrop
argument has an explicit&
. It's more reasonable for something likedrop(ref);
.
let _ = …
makes sense as a replacement when it actually drops something, e.g. for let _ = func_call()
. let _ = variable_containing_mut_ref;
is not useful as a replacement because it does exactly nothing, it simply matches the always matching pattern against the variable. The variable is not made inaccessible.
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 variable is not made inaccessible.
That was never the point. The point of the lint is to signal users that the drop
call does not drop the underline value.
Whenever or not the variable after the suggestion is made inaccessible is IMO completely irrelevant to the issue that the lint is trying to point at.
let _ = …
makes sense as a replacement when it actually drops something
Or as a way to suppress to the "unused variable" lint, as was called out by T-lang in #109732 (comment).
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 _ = …
makes sense as a replacement when it actually drops somethingOr as a way to suppress to the "unused variable" lint, as was called out by T-lang in #109732 (comment).
Yes. I think this should be a conscious decision though. let _ = variable;
signals to the reader that the programmer wanted to explicitly silence the unused variable lint. Perhaps we could mention that in the replacement hint? "If you just want to mark the variable as used, use let _ = variable
?
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.
Wasn't this already done through the help text: "help: use let _ = ...
to ignore the expression or result" ?
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 didn't understand the wording that way.
I'd go for something like "help: user let _ = …
to mark the variables as used". "to ignore the expression or result [or variable]" doesn't tell me (personally) that this is used to suppress the unused variable lint.
@Urgau, did you mention this to argue against the changes in the second commit? As per my comments above, I have doubts about both sets of changes here. |
More as a way to signal that the use of I'm also very sceptical of calling out (explicitly) the "inaccessibility" issue, I don't think it's relevant to most users. In fact the lint as been stable for many releases without people complaining (or even noticing) about this and I don't think we should complicate something that is (currently) relatively simple to understand with a more advanced notion of "moved variable for non-Copy types". |
@tbu- I think the best path forward here is:
|
☔ The latest upstream changes (presumably #125443) made this pull request unmergeable. Please resolve the merge conflicts. |
@tbu- any updates on this? thanks |
@tbu- |
Distinguish dropping mutable references and add more intelligent suggestions if a reference or copyable type is dropped: E.g. delete the expression if it is trivially side-effect free.
Fixes #125972.