Useless conditional¶
ID: js/trivial-conditionalKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - quality - reliability - correctness - external/cwe/cwe-570 - external/cwe/cwe-571Query 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 arraylines, and then attempts to check whether it has any elements by means of an if conditionalif(!lines).
functiongetLastLine(input){varlines=[],nextLine;while((nextLine=readNextLine(input)))lines.push(nextLine);if(!lines)thrownewError("No lines!");returnlines[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 checklines.length:
functiongetLastLine(input){varlines=[],nextLine;while((nextLine=readNextLine(input)))lines.push(nextLine);if(!lines.length)thrownewError("No lines!");returnlines[lines.length-1];}