Skip to content

Prevent equality checks as statements #59028

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

Closed
jonmountjoy opened this issue Feb 3, 2023 · 6 comments
Closed

Prevent equality checks as statements #59028

jonmountjoy opened this issue Feb 3, 2023 · 6 comments
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. P3 A lower priority bug or feature request type-question A question about expected behavior or functionality

Comments

@jonmountjoy
Copy link

Describe the rule you'd like to see added and to what rule set
I'd like to prevent this kind of bug:

...
itemState.learningAspect == LearningAspect.learnNumber;
...

In other words, I have a statement, and that statement is an equality check, the value of which I'm discarding.

It's almost always an error (for me, it's always an error), because what I invariably want to write is:

itemState.learningAspect = LearningAspect.learnNumber;

In other words, I've used an equality check (copy pasta or finger slip or whatever) instead of an assignment.

I can't imagine a scenario when anyone would want to write that kind of statement, but I guess it's possible. In other contexts (embedded as an expression), types would usually catch the error. But as a standalone statement, the type system is quite happy. I imagine at run time it performs the equality check and discards the boolean result.

In some ways I think it's similar to unawaited_futures. I wouldn't mind an "unused_boolean" - it would cover me in this situation and force me to think more about my other methods too.

Thoughts?

(thanks!)

@eernstg
Copy link
Member

eernstg commented Feb 3, 2023

Perhaps this should be transferred to the linter repository? It seems to be a potential enhancement of existing checks for statements with no observable effect (e.g., 25; or print;). I thought we already had some linter support for flagging such statements, but I can't see any matching lints at this time.

@pq, did we have a diagnostic for "code has no effect" (some special cases of that), but it was dropped again?

@bwilkerson bwilkerson transferred this issue from dart-archive/lints Feb 3, 2023
@bwilkerson
Copy link
Member

@srawlins Who might also remember whether we used to have such a diagnostic.

@pq
Copy link
Member

pq commented Feb 3, 2023

@eernstg, you may be thinking of unnecessary_statements?

@pq
Copy link
Member

pq commented Feb 3, 2023

In fact, @jonmountjoy: I think unnecessary_statements covers your case?

@pq pq added type-question A question about expected behavior or functionality P3 A lower priority bug or feature request labels Feb 3, 2023
@eernstg
Copy link
Member

eernstg commented Feb 3, 2023

Exactly, unnecessary_statements is what I was looking for, I just didn't come up with the right words to search for. ;-)

@jonmountjoy
Copy link
Author

Ahhh that's fabulous @eernstg @pq @bwilkerson - that does the job!
I would seriously consider putting this in the default recommended lints.

This is so nice - thank you!

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. P3 A lower priority bug or feature request type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

5 participants