quick-lint-js icon indicating copy to clipboard operation
quick-lint-js copied to clipboard

10$: warn on array equality check

Open cgsdev0 opened this issue 2 years ago • 5 comments

example code:

let arr = [];
if (arr === []) {
    // this can never happen
}

saw someone on twitch make this mistake in their code, when what they really wanted was arr.length === 0

maybe quicklint should warn that the expression arr === [] here is always false?

cgsdev0 avatar Jun 29 '22 17:06 cgsdev0

This diagnostic is a good idea. === {} is similar.

strager avatar Jun 29 '22 17:06 strager

broader question: should it just complain for any array or object literal?

cgsdev0 avatar Jun 29 '22 18:06 cgsdev0

Yes. The comparison will always fail.

strager avatar Jun 29 '22 18:06 strager

can i still work on this and what folder or file should I open to make the changes

nebulahh avatar Jul 23 '22 13:07 nebulahh

can i still work on this

Yes.

what folder or file should I open to make the changes

In test/test-parse-warning.cpp there are tests for similar cases. You can copy-paste and tweak as needed.

In src/quick-lint-js/fe/parse.cpp there's a parser::error_on_sketchy_condition function. That might be a good place to write the implementation.

src/quick-lint-js/fe/expression.h contains definitions for expression classes.

strager avatar Jul 23 '22 18:07 strager

I think this now duplicates https://github.com/quick-lint/quick-lint-js/issues/821

cgsdev0 avatar Sep 14 '22 01:09 cgsdev0

@cgsdev0 Oh, you're right!

strager avatar Sep 14 '22 06:09 strager

@nebulahh You haven't showed progress on this task, and @Rebraws wrote a PR fixing this. I'm going to close this duplicate task and leave @Rebraws assigned.

Sorry for the confusion. I hope you didn't put in too much work already. =X

strager avatar Sep 14 '22 06:09 strager