Skip to main content
ai-slop/csharp-index-loopmanual fix

Index for-loop.

`for (int i = 0; i < items.Length; i++)` where `i` is only used to read `items[i]`. A `foreach` with extra plumbing.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

csharp-index-loop.cs
✗ Flagged
for (int i = 0; i < orders.Count; i++)
{
    Ship(orders[i]);
}
Why this trips

Deterministic loop parse: flags zero-based `for` loops bounded by `.Length` or `.Count` and incremented by one, when the body uses the index only inside `[i]` subscripts.

csharp-index-loop.cs
✓ Clean
foreach (var order in orders)
{
    Ship(order);
}
What changed

Use `foreach (var item in items)`. Keep the index loop when the position itself matters.

Agents default to the index-loop shape common across C-family languages, even when no index is needed.

How this rule is justified.

Rule id

ai-slop/csharp-index-loop

Enforcing engine

ai-slop

Detector strategy

Deterministic loop parse: flags zero-based `for` loops bounded by `.Length` or `.Count` and incremented by one, when the body uses the index only inside `[i]` subscripts.

Legitimate code that is NOT flagged

Loops that use the index for anything else (arithmetic, output, passing it on), loops with other bounds or steps, and `foreach` loops are not flagged.

Evidence

Part of aislop's C# rule set. First-class C# support, including C#-specific AI-slop rules, arrived in aislop v0.14.1.