question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should we extend no-constant-condition to catch comparisons that are constant due to type mismatch?

See original GitHub issue

What rule do you want to change?

no-constant-condition

Does this change cause the rule to produce more or fewer warnings?

More

How will the change be implemented? (New option, new default behavior, etc.)?

New default behavior

Please provide some example code that this change will affect:

(a + b ?? c)

(!foo == null)

(!foo ?? bar)

((a + b) / 2 ?? bar)

const bar = String(foo); (bar == null)

(String(foo.bar) ?? baz)

const bar = <div>Hi!</div>; (bar == null)

(<div>Hi!</div> ?? foo)

("hello" + name ?? "")

([foo?.bar ?? ""] ?? [])

What does the rule currently do for this code?

Nothing

What will the rule do after it’s changed?

Warn/Error

Are you willing to submit a pull request to implement this change?

Possibly

No Useless Null Checks

There are some types of comparisons which we can tell statically will always create the same result. One such example is performing a null check on an ObjectExpression. In many cases, these type of coding errors are harmless since they merely guard against edge cases which can’t actually exist. However, in other cases they represent a misunderstanding of how the code will behave. For example:

  • a + b ?? c Misunderstanding of operator precedence.
  • <SomeComponent /> ?? <Fallback /> Incorrect assumption about JSX returning the result of render function

no-constant-condition guards against many of these types of errors but it does not currently try to understand comparisons that are constant due to the statically knowable type of the values being compared.

My question is: should it?

On one hand this seems like these are constant conditions that ESLint can determine, so it should! On the other hand, if ESLint wanted to report all possible constant conditions of this sort, it would end up implementing a type system, which is clearly out of scope for this project.

Speaking of type systems, it’s worth noting that many of these errors, type systems (Flow and TypeScript) do not currently report as errors.

Faced with the question of “how far should a rule like this go?” I decided to pick an arbitrary subset of “constant comparisons to null” and write a rule specifically to check for that. You can find it here.

Surprisingly it found hundreds of errors in our (very large) code base, so I believe the rule has some non-trivial value.

I would be curious to hear any maintainer’s thoughts on if this would be a good addition to no-constant-condition, of if it seems out of scope in some way.

If the rule that checks for constant null comparisons seems like a good addition, then it might be worth considering other similar checks that could be performed, such as constant boolean comparisons: {} || fallback.

Thanks to @bradzacher for some initial insight which lead to this rule.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:29 (28 by maintainers)

github_iconTop GitHub Comments

4reactions
ljharbcommented, Oct 14, 2020

That’s a really good point, thanks for clarifying.

3reactions
captbaritonecommented, Oct 16, 2021

I can work on this. Busy this weekend, but can pick it up next week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I get a message about data type mismatch - Microsoft Support
You get a "Data Type Mismatch" error when you run a query. ... marks around the Default Value expression (e.g., "1" would evaluate...
Read more >
Why do I receive a data type mismatch error when using a ...
My model contains a Model Reference block where the referenced model inport data type is set to AUTO. The referenced model uses the...
Read more >
VBA - Type Mismatch (Run-time Error 13) - Automate Excel
The code tries to assign the value to the variable 'MyNumber' which has been defined to expect an integer, and so you get...
Read more >
Type checking errors - Free Pascal
This section lists all errors that can occur when type checking is performed. Error: Type mismatch: This can happen in many cases: The...
Read more >
Why do I get a type mismatch error comparing two strings
Your Answer. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found