-
Notifications
You must be signed in to change notification settings - Fork 30
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
Consider adding unnecessary_statements
to recommended
#104
Comments
The lint is under-documented, so I have no idea what it actually triggers on. tear-offs are known to not have side-effects. Anything that could be a constant expression too. (There are a lot of functions whose primary purpose is to create a new object. If that object isn't used, the call is useless, but we cannot tell that from method signatures. And the traditional I need more documentation and specification before I'll consider the lint. |
Mistaken tear-offs are a big motivator for getting this lint into the recommended set. If we think we could define a more narrow lint without false positives, and recommend that one, I'd be in favor. I think we could also flag @pq WDYT about looking for a second no-false-positive version of this lint? I think the original version is likely still useful for folks using it already. IIRC getters were a bigger motivator than tear-offs. |
If we can increase adoption, I'm up for a variation on this lint. On the other hand, while this is true
I wonder, in practice, how much of an issue this is. Depending on the side-effects of an (otherwise) apparently unnecessary statement seems like a code smell to me. |
Yes, I agree it's a code smell, and the existing lint is worthy for consideration in a "strict" set of rules. I don't think the current rule is feasible in a recommended set, but a more narrow rule shouldn't be controversial. |
I think there is room for two lints here: A "this code definitely does nothing" and a "this code looks like it does nothing". The first should have no false positives. It'd probably be something like an expression in a context where the value isn't used, which:
All of these create or reference a value, with no possible side effect, and then throws the value away again. If we allow subexpressions to have effects, it would would include any map/set/list literal, even if elements have side-effects. The allocation itself is unnecessary. The second lint would look at expected behavior for certain operations, and try to enforce that the syntax matches the expected semantics, not just the actual semantics. It would include:
And we can include extra class-specific knowledge about methods with no (expected) side effects:
Those are operations where some can be used for side-effects, but you shouldn't. Your code looks like it does nothing, so it should do nothing. If it does nothing, you should remove it. If it does something, you should rewrite it to look like it does something, so it's not misleading readers. |
This lint will likely need more investigation. Generally, we would be in favor of a version of this lint with no false positives (flagging unnecessary statements where the code for the statement should generally be side-effect free). |
This lint does tend to help catch mistakes, but it has a small false positive rate because there are legitimate cases to call a getter for it's side effects.
We had some discussion about an annotation to mark getters as having side effects to suppress this lint, but nothing has landed.
dart-lang/sdk#35513
It seems like the current practice is to use
// ignore:
comments, and we've so far avoided adding lints with known false positives that can't be suppressed in a nicer way than an ignore.The text was updated successfully, but these errors were encountered: