pi-lens
Version:
Real-time code feedback for pi — LSP, linters, formatters, type-checking, structural analysis & booboo
41 lines (32 loc) • 1.11 kB
YAML
# Loose equality in for-loop termination
# Detects ==/!= used as a for-loop's exit test, which is fragile.
id: no-equality-in-for-condition
name: Loose equality in for-loop condition
severity: warning
category: bug
inline_tier: warning
language: typescript
message: "for-loop uses ==/!= for termination — prefer </<=/>/>= so stepping past the bound still exits"
description: |
Using == or != as a for-loop's termination test is fragile: if the counter
steps past the target value (e.g. i += 2, or it is mutated in the body), the
equality never holds and the loop runs forever. Use a relational operator
(<, <=, >, >=) so the loop still terminates when the counter overshoots.
❌ for (let i = 0; i != n; i += 2) // infinite if n is odd
✅ for (let i = 0; i < n; i += 2)
query: |
(for_statement
(expression_statement
(binary_expression
["==" "!="] @OP)))
metavars:
- OP
has_fix: false
tags:
- bug
- loop
examples:
bad: |
for (let i = 0; i != items.length; i += 2) { /* ... */ }
good: |
for (let i = 0; i < items.length; i += 2) { /* ... */ }