Skip to content

Linter for length(x == y) where almost certainly length(which(x == y)) or length(x) == y was intended #1991

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
MichaelChirico opened this issue Jun 30, 2023 · 4 comments · Fixed by #2124
Milestone

Comments

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Jun 30, 2023

This is a mistake that I find myself making all the time:

if (length(x == 0)) ...

It makes itself into a really subtle bug and many wasted minutes.

I can't think of a case where length(x == y) is preferable to length(x) or length(y), unless there's some weird coercion going on and that should be made explicit anyway.

Therefore it seems like a good candidate for a new linter.

I don't really know what to name such a linter... I also wonder if maybe it's good for an umbrella linter like common_mistakes_linter(), though I can't really think of other good candidate rules for that off the top of my head. see comment below

@AshesITR
Copy link
Collaborator

How about length_equals_linter()?
I imagine the most common intention is length(x) == y.

I don't think we should make an umbrella linter.

@MichaelChirico
Copy link
Collaborator Author

Note to self: the right way to group this is with the tag common_mistakes.

@AshesITR
Copy link
Collaborator

AshesITR commented Aug 8, 2023

BTW the length(which()) case should be replaced by sum().

@MichaelChirico MichaelChirico added this to the 3.1.1 milestone Aug 22, 2023
@MichaelChirico
Copy link
Collaborator Author

BTW the length(which()) case should be replaced by sum().

I think that's a separate linter... there's some nuance there, as length(which(A)) is equivalent to sum(A) but the most common case will be comparing length(which(A)) to 0, in which case any(A) is preferable, which is boolean_arithmetic_linter().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants