-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add lint for unused_result_ok
#12150
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dswij (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
323da92
to
2893485
Compare
let ctxt = expr.span.ctxt(); | ||
let mut applicability = Applicability::MachineApplicable; | ||
let trimmed_ok = snippet_with_context(cx, recv.span, ctxt, "", &mut applicability).0; | ||
let sugg = format!("let _ = {}", trimmed_ok.trim().trim_end_matches('.'),); |
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 does the trimming here do?
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'm not 100% sure.
I used matching_result_ok as an example.
IIUC it removes the extra spaces on cases like line 14 of the UI test sample.
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.
🤔 That's odd, I'm not sure what's going on in match_result_ok. The span of the receiver of the .ok()
method call should always be
x . parse::<i32>() . ok ();
^^^^^^^^^^^^^^^^^^^^^^
I tried removing the trimming in both this lint and in match_result_ok and in both cases it seems to pass all tests just fine.
@flip1995 seeing that you reviewed #5032, which is where that was introduced, do you know why this was necessary?
Specifically referring to this line
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.
No idea anymore. My suspicion is, that back then I/we cared more about well formatted suggestions. Nowadays, we don't really care about that so much, as we assume that everyone uses rustfmt
anyway and Clippy suggestion formatting is not important.
But that is just a wild guess.
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 there any compatibility requirement with previous version of the compiler?
As far as I understand, _ =
is a destructuring assignment introduced in 1.59.
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.
Do you mean in the Clippy codebase or in the suggestions made by Clippy?
In the former case: no
In the latter case: Yes. If you want to add a suggestion that suggests _ = ...
, it should probably be gated by an MSRV check for 1.59.0
where that feature was added. There's a lot of examples in the code of how to do that, should it be required here.
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 meant the suggestion. So yes, let’s keep it as let _ = …
.
I’ll let you resolve this conversation if it the required changes have been met.
I discovered today that |
FME, |
I’ll keep it as I don’t think having a different recommendation based on the compiler version in used is a great things. The noise caused would outweigh the subjective readability benefits. |
I agree with @dswij, and FWIW, the rustc lint |
beee858
to
c0c5a76
Compare
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.
Sorry for the long wait. Implementation LGTM, I just have some small documentation nits, then IMO it's merge ready? @dswij do you have something else, since you're assigned?
We could also just add So, probably better to have it as a clippy lint instead. |
This definitely looks like a case for |
It probably makes sense to open a discussion for this on zulip. I view rustc's I think adding |
Well, it has been another 2 months~ so, pinging the most recent people who replies (how you don't mind): @y21 @xFrednet, anyone wanna have the privilege to open a discussion/FCP on zulip ? 😄 At the mean time, @ithinuel, sorry for the long wait, do you mind applying those suggestions made by |
0b8cee3
to
b4ba7ee
Compare
I can give this another look, if @y21 doesn't get to it during the weekend :) |
Sorry, missed this. Implementation wise this all still looks good to me. There's still the question if we want this in the first place. |
See also rust-lang/rust#66116 |
Looks like people are generally on board with having this be a clippy lint, based on the FCP thread. The discussion on the issue that Alexendoo linked also shows that it is a pattern that people write and would probably be too strong for rustc to warn on as part of unused_must_use for now, and it's also been tried before in rust-lang/rust#79006 and got rejected. @flip1995 on Zulip even went as far as wanting this in the restriction category (ie allow-by-default, not (There's also the concern that this lint will need to be deprecated when that method gets marked as |
Alright, the poll didn't bring in a lot more opinions but we did get another vote for restriction, so that sounds like we should go with that. |
2662813
to
d3d567c
Compare
That should do the trick (I also rebased on top of master). |
There's still a bunch of commits that don't seem meaningful outside of this PR (eg "apply review feedback", "remove trim", "rename and add tests", ...). Can you squash those also? |
Also @dswij , I forgot that you're still also assigned to this PR. Did you have anything else? |
d3d567c
to
182c268
Compare
@y21, I squashed the commits together and kept the relevant commit title. |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
This PR adds a lint to capture the use of
expr.ok();
when the result is not really used.This could be interpreted as the result being checked (like it is with
unwrap()
orexpect
) butit actually only ignores the result.
let _ = expr;
expresses that intent better.This was also mentionned in #8994 (although not being the main topic of that issue).
changelog: [
misleading_use_of_ok
]: Add new lint to capture.ok();
when the result is not really used.