-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Warning for unused arithmetic expressions #50124
Comments
Gaaaaaaaaaaaaah. rust-lang/rust#50124
We should just be able to add |
As an implementation detail, the operators are hardcoded in the lint (while the corresponding trait methods also take the attribute for when they are called as methods). |
It would be trivial to add
Why are we matching on the exact binary operations there anyway? All of them should be triggering the lint. All the op-eq operators are encoded differently, right, so there shouldn't be any false-positives. This seems like a good beginner bug actually. I have more experience with macro expansion than HIR but I'm willing to mentor if no one else wants to. @kennytm |
From #48926, I think the idea was to be conservative to begin with, but it seems very reasonable that non-assigning operators should be |
@abonander I'm actually not terribly worried about |
@jonhoo It's mostly for consistency and But as I mentioned, the lint is actually excluding the arithmetic operators right now; it would be less complex if it linted on all binary operators unconditionally (excluding op-assign which is encoded in the AST/HIR as a separate expression kind anyway). |
Mentoring InstructionsThis one should be pretty straightforward. Right now the only thing being checked by the This is caused by this pattern match that only emits the lint error for the comparison operators. We should keep the match but use it to select the lint message. Additionally, When that's done I believe lint warnings are tested with UI tests. |
Hey @abonander, I've started working on this issue but I just want to clarify a few things. When you say to add Thanks! Edit: |
That note intentionally only appears on the first warning for a given lint. |
Oh, in that case I'll just make sure this passes the tests and open a pull request so you all can check it out. |
Added warning for unused arithmetic expressions The compiler now displays a warning when a binary arithmetic operation is evaluated but not used. This resolves #50124 by following the instructions outlined in the issue. The changes are as follows: - Added new pattern matching for unused arithmetic expressions in `src/librustc_lint/unused.rs` - Added `#[must_use]` attributes to the binary operation methods in `src/libcore/internal_macros.rs` - Added `#[must_use]` attributes to the non-assigning binary operators in `src/libcore/ops/arith.rs`
After having spent a day tracking down this bug, and recalling all the past times the same thing has bitten me, I'd like to propose a lint unused results of arithmetic expressions. Consider the following code:
This code currently compiles with no warnings (given a suitable
enum Foo
). However, bugs like these can be extremely hard to track down. They can often be hard to spot, especially when going through methods likesaturating_sub
which do not modifyself
, but instead return the new result (e.g.,delta = delta.saturating_sub(x)
is necessary).It would be really neat to have a warning about unused arithmetic expressions (ideally one that also handles
saturating_sub
). I don't think there are generally cases where not using the result of an arithmetic expression is what the developer intended to do. This also complements theunused_variables
andunused_mut
lints quite nicely.The text was updated successfully, but these errors were encountered: