CodeQL documentation

Useless comparison test

ID: js/useless-comparison-test
Kind: problem
Security severity: 
Severity: warning
Precision: high
Tags:
   - correctness
Query suites:
   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

If a condition always evaluates to true or always evaluates to false, this often indicates incomplete code or a latent bug, and it should be examined carefully.

Recommendation

Examine the surrounding code to determine why the condition is redundant. If it is no longer needed, remove it.

If the check is needed to guard against NaN values, insert a comment explaining the possibility of NaN.

Example

The following example finds the index of an element in a given slice of the array:

function findValue(values, x, start, end) {
  let i;
  for (i = start; i < end; ++i) {
    if (values[i] === x) {
        return i;
    }
  }
  if (i < end) {
    return i;
  }
  return -1;
}

The condition i < end at the end is always false, however. The code can be clarified if the redundant condition is removed:

function findValue(values, x, start, end) {
  for (let i = start; i < end; ++i) {
    if (values[i] === x) {
        return i;
    }
  }
  return -1;
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy