Skip to main content

no-unmodified-loop-condition

Loop conditions that are never modified in the loop body usually indicate a mistake.
Rule Type: Problem
Fixable: No

Why This Rule Exists

If a variable appears in a loop condition but is never modified inside the loop, the loop will either never execute or become infinite. This is almost always a bug.

Rule Details

This rule finds references in loop conditions and checks whether the variables are modified in the loop body. What it checks:
  • Variables in loop conditions
  • Binary expressions in conditions
  • Ternary expressions in conditions
What it ignores:
  • Dynamic expressions (function calls, yield, etc.)
  • Object properties (they might have getters)

Examples

Incorrect Code

Correct Code

Common Patterns

Linked List Traversal

Array Iteration

Breaking Out of Loop

Why Properties and Functions Are Ignored

Properties and function calls are not checked because they might have side effects or dynamic behavior.

Properties Might Be Getters

Functions Can Examine State

Fixing Infinite Loops

Update the Condition Variable

Use a Different Loop Type

Add Proper Exit Condition

When Not to Use It

You might want to disable this rule if:
  1. Your code intentionally uses infinite loops with explicit breaks
  2. You have complex loop conditions that the rule can’t analyze correctly
However, in most cases, these patterns can be refactored to be clearer.