google / google/closure-compiler
Confusing error message for deterministic equality check
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
[repro]
The following error message is a bit confusing:
```js
/** @constructor */
function Foo() {}
/** @constructor */
function Bar() {}
function f(/** ?Foo */ foo, /** ?Bar */ bar) {
if (foo === bar) throw '';
}
```
```
input0:7: WARNING - condition always evaluates to false
left : (Foo|null)
right: (Bar|null)
if (foo === bar) throw '';
^^^^^^^^^^^
```
This is inaccurate - the condition does *not* always evaluate to false. It *is* suspicious code and probably not what the user intended, but to be completely accurate we should check if the two sides both include null/undefined and if that's the case then add a bit about "for non-trivial values", or say "instead check foo == null && bar == null" or something.
[repro]: https://closure-compiler-debugger.appspot.com/#input0%3D%252F**%2520%2540constructor%2520*%252F%250Afunction%2520Foo()%2520%257B%257D%250A%252F**%2520%2540constructor%2520*%252F%250Afunction%2520Bar()%2520%257B%257D%250A%250Afunction%2520f(%252F**%2520%253FFoo%2520*%252F%2520foo%252C%2520%252F**%2520%253FBar%2520*%252F%2520bar)%2520%257B%250A%2520%2520if%2520(foo%2520%253D%253D%253D%2520bar)%2520throw%2520''%253B%250A%257D%26input1%26conformanceConfig%26externs%26refasterjs-template%26CHECK_SYMBOLS%3Dtrue%26MISSING_PROPERTIES%3Dtrue%26TRANSPILE%3Dtrue%26CHECK_TYPES%3Dtrue%26CLOSURE_PASS%3Dtrue%26PRESERVE_TYPE_ANNOTATIONS%3Dtrue%26PRETTY_PRINT%3Dtrue
Contributor guide
Assessment
This issue has not been assessed yet.