Collection size or array length compared against an impossible bound
ID |
vbnet.correctness.meaningless_size_comparison |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
VB.NET |
Description
Flags a comparison between a Count, LongCount, Length or LongLength member and 0 or
-1 whose outcome is fixed: < 0 and ⇐ -1 can never be True, while >= 0 and > -1 are
always True. Both operand orders are matched, so 0 > slots.Length is reported as well as
slots.Length < 0. The rule keys on the member name of a qualified access, so it sees
Lines.Count < 0 and grid.LongLength < 0. Meaningful tests are untouched: = 0 and < 1
for empty, > 0 for non-empty, and comparisons against a genuine bound such as > 100.
Rationale
A collection count and an array length are non-negative by definition, so the comparison cannot be expressing the check the author intended. An impossible test leaves its body permanently dead: a validation that never rejects anything, an error path that never runs, so the malformed input it was written to catch passes straight through. An always-True test is the mirror image — the guard admits everything, and the code behind it runs against the empty collection it was supposed to be protected from, usually failing later on an index or a first element, well away from the guard that let it through. In both cases the code reads as though the case were handled, which is exactly why the mistake survives review.
The following code illustrates the pattern detected by this rule:
Public Function IsCorrupt() As Boolean
' FLAGGED: Collection size or array length compared against an impossible bound
If Lines.Count < 0 Then
Return True
End If
Return False
End Function