- What language is this for? JavaScript
- Which rule? S1940
- Why do you believe it’s a false-positive/false-negative?
NaN < 0
andNaN >= 0
are both false, so suggesting to change!(x < 0)
tox >= 0
may change the behaviour of the code ifx
is floating point (which is almost always the case in JavaScript). What’s more, many people do not know this detail of floating point in the IEEE 754 standard. - Are you using
- SonarCloud? yes
- How can we reproduce the problem?
function pauseIfAppropriate(video) {
if (!(video.duration < Infinity)) {
return false;
}
if (!video.paused) {
video.pause();
}
return true;
}
notes
It looks like The inverted boolean-check introduces subtle mistakes · Issue #211 · SonarSource/eslint-plugin-sonarjs · GitHub deactivated this rule by default, but I still see S1940 in the Sonar Way in SonarCloud.
There is no issue with the ==
, ===
, !=
and !==
operators. NaN is not equal to itself but the !=
and !==
operators properly reflect that.
The issue might also occur in other languages, but not as commonly as in JavaScript and TypeScript which implicitly create NaN from converting a non-numeric string to a number and have floating point as the most common type of number.
There is also no issue if neither side can be a number or boolean, but in most cases it is not possible to be sure (!('' + x < 'A')
would be such a rare case where it is possible).
The negation could be removed by adding a call to isNaN
(or Number.isNaN
with an explicit +
conversion to number) to keep the behaviour the same, but that may not be an improvement.