CodeQL documentation

Useless conditional

ID: js/trivial-conditional
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - correctness
   - external/cwe/cwe-570
   - external/cwe/cwe-571
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 should be examined carefully.

Recommendation

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

Example

The following example constructs an array lines, and then attempts to check whether it has any elements by means of an if conditional if (!lines).

function getLastLine(input) {
  var lines = [], nextLine;
  while ((nextLine = readNextLine(input)))
    lines.push(nextLine);
  if (!lines)
    throw new Error("No lines!");
  return lines[lines.length-1];
}

Note that in JavaScript (unlike some other languages) arrays and objects are always considered to be true when evaluated in a Boolean context. The code should instead check lines.length:

function getLastLine(input) {
  var lines = [], nextLine;
  while ((nextLine = readNextLine(input)))
    lines.push(nextLine);
  if (!lines.length)
    throw new Error("No lines!");
  return lines[lines.length-1];
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy