Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 08d9af1

Browse files
authored
Merge pull request #280 from owen-mc/negative-length-check-unsigned
Extend negativeLengthCheck query to unsigned integers
2 parents 117fd68 + 97bbdca commit 08d9af1

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lgtm,codescanning
2+
* Query "Redundant check for negative value" (`go/negative-length-check`) has been expanded to consider unsigned integers, along
3+
with the return values of `len` and `cap` which it already handled. It has also been renamed to match its expanded role.

ql/src/RedundantCode/NegativeLengthCheck.qhelp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ never less than zero. Hence, checking whether the result of a call to <code>len<
1010
is either redundant or indicates a logic mistake.
1111
</p>
1212
<p>
13-
The same applies to the built-in function <code>cap</code>.
13+
The same applies to the built-in function <code>cap</code>, and to unsigned integer values.
1414
</p>
1515
</overview>
1616

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
2-
* @name Check for negative length
2+
* @name Redundant check for negative value
33
* @description Checking whether the result of 'len' or 'cap' is negative is pointless,
4-
* since these functions always returns a non-negative number.
4+
* since these functions always returns a non-negative number. It is also
5+
* pointless checking if an unsigned integer is negative, as it can only
6+
* hold non-negative values.
57
* @kind problem
68
* @problem.severity warning
79
* @precision very-high
@@ -11,12 +13,19 @@
1113

1214
import go
1315

14-
from ComparisonExpr cmp, BuiltinFunction len, int ub, string r
16+
from ComparisonExpr cmp, DataFlow::Node op, int ub, string d, string r
1517
where
16-
(len = Builtin::len() or len = Builtin::cap()) and
18+
(
19+
exists(BuiltinFunction bf | bf = Builtin::len() or bf = Builtin::cap() |
20+
op = bf.getACall() and d = "'" + bf.getName() + "'"
21+
)
22+
or
23+
op.getType().getUnderlyingType() instanceof UnsignedIntegerType and
24+
d = "This unsigned value"
25+
) and
1726
(
1827
exists(RelationalComparisonExpr rel | rel = cmp |
19-
rel.getLesserOperand() = len.getACall().asExpr() and
28+
rel.getLesserOperand() = op.asExpr() and
2029
rel.getGreaterOperand().getIntValue() = ub and
2130
(
2231
ub < 0
@@ -27,10 +36,10 @@ where
2736
)
2837
or
2938
exists(EqualityTestExpr eq | eq = cmp |
30-
eq.getAnOperand() = len.getACall().asExpr() and
39+
eq.getAnOperand() = op.asExpr() and
3140
eq.getAnOperand().getIntValue() = ub and
3241
ub < 0 and
3342
r = "equal"
3443
)
3544
)
36-
select cmp, "'" + len.getName() + "' is always non-negative, and hence cannot " + r + " " + ub + "."
45+
select cmp, d + " is always non-negative, and hence cannot " + r + " " + ub + "."

ql/test/query-tests/RedundantCode/NegativeLengthCheck/NegativeLengthCheck.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
| main.go:14:5:14:20 | ...<... | 'cap' is always non-negative, and hence cannot be less than 0. |
44
| main.go:18:5:18:22 | ...<=... | 'len' is always non-negative, and hence cannot be less than -1. |
55
| main.go:22:5:22:22 | ...==... | 'len' is always non-negative, and hence cannot equal -1. |
6+
| main.go:28:9:28:13 | ...<... | This unsigned value is always non-negative, and hence cannot be less than 0. |
7+
| main.go:36:9:36:15 | ...==... | This unsigned value is always non-negative, and hence cannot equal -1. |

ql/test/query-tests/RedundantCode/NegativeLengthCheck/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ func main() {
2323
println("No arguments provided.")
2424
}
2525
}
26+
27+
func checkNegative(x uint) bool {
28+
return x < 0 // NOT OK
29+
}
30+
31+
func checkNonPositive(x uint) bool {
32+
return x <= 0 // OK
33+
}
34+
35+
func checkIsMinusOne(x uint) bool {
36+
return x == -1 // NOT OK
37+
}

0 commit comments

Comments
 (0)