Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 344bec3

Browse files
committed
JS: Add UselessRangeCheck.ql
1 parent d813635 commit 344bec3

8 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
If a condition always evaluates to true or always evaluates to false, this often indicates
8+
incomplete code or a latent bug and should be examined carefully.
9+
</p>
10+
11+
</overview>
12+
<recommendation>
13+
14+
<p>
15+
Examine the surrounding code to determine why the condition is useless. If it is no
16+
longer needed, remove it.
17+
</p>
18+
19+
<p>
20+
If the check is needed to guard against NaN values, insert a comment explaning the possibility of NaN.
21+
</p>
22+
23+
</recommendation>
24+
<example>
25+
26+
<p>
27+
The following example finds the index of an element in a given slice of the array:
28+
</p>
29+
30+
<sample src="examples/UselessConditional.js" />
31+
32+
<p>
33+
The condition <tt>i &lt; end</tt> at the end is always false, however. The code can be clarified if the
34+
redundant condition is removed:
35+
</p>
36+
37+
<sample src="examples/UselessConditionalGood.js" />
38+
39+
</example>
40+
<references>
41+
42+
43+
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">Truthy</a>,
44+
<a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">Falsy</a>.</li>
45+
46+
</references>
47+
</qhelp>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Useless range check
3+
* @description If a range check always fails or always succeeds it is indicative of a bug.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id js/useless-range-check
7+
* @tags correctness
8+
* @precision high
9+
*/
10+
11+
import javascript
12+
13+
/**
14+
* Gets the guard node with the opposite outcome of `guard`.
15+
*/
16+
ConditionGuardNode getOppositeGuard(ConditionGuardNode guard) {
17+
result.getTest() = guard.getTest() and
18+
result.getOutcome() = guard.getOutcome().booleanNot()
19+
}
20+
21+
from ConditionGuardNode guard
22+
where RangeAnalysis::isContradictoryGuardNode(guard)
23+
24+
// Do not report conditions that themselves are unreachable because of
25+
// a prior contradiction.
26+
and not RangeAnalysis::isContradictoryGuardNode(getOppositeGuard(guard))
27+
28+
select guard.getTest(), "The condition '" + guard.getTest() + "' is always " + guard.getOutcome().booleanNot()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function findValue(values, x, start, end) {
2+
let i;
3+
for (i = start; i < end; ++i) {
4+
if (values[i] === x) {
5+
return i;
6+
}
7+
}
8+
if (i < end) {
9+
return i;
10+
}
11+
return -1;
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function findValue(values, x, start, end) {
2+
for (let i = start; i < end; ++i) {
3+
if (values[i] === x) {
4+
return i;
5+
}
6+
}
7+
return -1;
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| example.js:8:7:8:13 | i < end | The condition 'i < end' is always false |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Statements/UselessRangeCheck.ql
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function findValue(values, x, start, end) {
2+
let i;
3+
for (i = start; i < end; ++i) {
4+
if (values[i] === x) {
5+
return i;
6+
}
7+
}
8+
if (i < end) {
9+
return i;
10+
}
11+
return -1;
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function findValue(values, x, start, end) {
2+
for (let i = start; i < end; ++i) {
3+
if (values[i] === x) {
4+
return i;
5+
}
6+
}
7+
return -1;
8+
}

0 commit comments

Comments
 (0)