-
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
add comparison operators to must-use lint (under fn_must_use
feature)
#44103
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc_lint/unused.rs
Outdated
|
||
if let hir::ExprBinary(bin_op, ..) = expr.node { | ||
match bin_op.node { | ||
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe | hir::BiGt => { |
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 think it would be less surprising, if instead of whitelisting operators, we'd check whether the corresponding operator method has the attribute.
src/test/ui/lint/fn_must_use.stderr
Outdated
warning: unused comparison which must be used | ||
--> $DIR/fn_must_use.rs:49:7 | ||
| | ||
49 | 2 == 3; |
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 result of the comparison is unused. The span should be including the rhs and lhs of the operation
r=pnkfelix once @oli-obk 's points are addressed. |
Hi @zackmdavis. Looks like you just need to make a few changes to get this over to ship. Just a friendly ping to keep this on your radar. |
@arielb1 Yes, thanks: I spent a couple hours last week trying to figure out how to look up the method definition given the binary-operator HIR node, and couldn't figure out how to make it work; I want to take another look sometime this week before I stoop to the indignity of begging for mentoring. |
You can get the To obtain the attributes, you can check if
I'm horrified we present the image that any form of mentoring requires begging or is indignified. Please don't hesitate to ask for help either on irc, here or on the internals forum, no matter how trivial you think the topic might be. It's likely it's not that trivial (like in this case, it's actually impossible to obtain the method's HIR in general, because the method is very likely in another crate (e.g. libstd or libcore)). |
Hi @zackmdavis, just wanted to check in to make sure this PR is still on your radar! Please do reach out for a hand via the methods @oli-obk mentioned if it'd help at all. |
Regret the delay; I'll set aside some time Friday. (I swear I tried |
I see—if both operands are scalars, we don't fill (Existing PR revision will collide with #44620, which I expect to be rolled up.) |
We don't; please construe that somewhat tongue-in-cheek remark as reflecting my personal (and, one could argue, neurotic and maladaptive) pride in my ability to RTFM. |
Hmm... I can't figure out why we remove scalars from the map... not even sure who to ping. That code is older than two years |
Looks like d649292. The pull request (by @nikomatsakis) says:
|
Ah yeah that. I myself don't see a problem with keeping an hard list of binary operators. |
If you wanted to avoid the hardcoding, you could move this part of rust/src/librustc_typeck/check/op.rs Lines 362 to 421 in ef227f5
and then call it on the binary operator to get the defid of the op method. |
☔ The latest upstream changes (presumably #44620) made this pull request unmergeable. Please resolve the merge conflicts. |
Although RFC 1940 is about annotating functions with `#[must_use]`, a key part of the motivation was linting unused equality operators. (See https://github.com/rust-lang/rfcs/pull/1812#issuecomment-265695898—it seems to have not been clear to discussants at the time that marking the comparison methods as `must_use` would not give us the lints on comparison operators, at least in (what the present author understood as) the most straightforward implementation, as landed in rust-lang#43728 (3645b06).) To rectify the situation, we here lint unused comparison operators as part of the unused-must-use lint (feature gated by the `fn_must_use` feature flag, which now arguably becomes a slight (tolerable in the opinion of the present author) misnomer). This is in the matter of rust-lang#43302.
c057542
to
8917616
Compare
(rebased) fixed comparison expression span, conflict with master |
Anyone else have an opinion? My disposition is to merge the most recent revision that I just pushed that leaves the hardcoding (with a comment noting the judgement call). @arielb1's idea of pulling code out of |
I'm fine with hardcoding, as long as there are sufficient comments explaining the haedcoding. And as long as it works for nonprimitives with the trait attribute check. |
Nice! @bors r+ |
📌 Commit 6734d39 has been approved by |
add comparison operators to must-use lint (under `fn_must_use` feature) Although RFC 1940 is about annotating functions with `#[must_use]`, a key part of the motivation was linting unused equality operators. (See https://github.com/rust-lang/rfcs/pull/1812#issuecomment-265695898—it seems to have not been clear to discussants at the time that marking the comparison methods as `must_use` would not give us the lints on comparison operators, at least in (what the present author understood as) the most straightforward implementation, as landed in rust-lang#43728 (3645b06).) To rectify the situation, we here lint unused comparison operators as part of the unused-must-use lint (feature gated by the `fn_must_use` feature flag, which now arguably becomes a slight (tolerable in the opinion of the present author) misnomer). This is in the matter of rust-lang#43302. cc @crumblingstatue
Although RFC 1940 is about annotating functions with
#[must_use]
, akey part of the motivation was linting unused equality operators.
(See
https://github.com/rust-lang/rfcs/pull/1812#issuecomment-265695898—it
seems to have not been clear to discussants at the time that marking the
comparison methods as
must_use
would not give us the lints oncomparison operators, at least in (what the present author understood
as) the most straightforward implementation, as landed in #43728
(3645b06).)
To rectify the situation, we here lint unused comparison operators as
part of the unused-must-use lint (feature gated by the
fn_must_use
feature flag, which now arguably becomes a slight (tolerable in the
opinion of the present author) misnomer).
This is in the matter of #43302.
cc @crumblingstatue