Skip to main content
ai-slop/csharp-linq-countmanual fix

Count() emptiness check.

`.Count() > 0` or `.Count() == 0` used to ask whether a sequence has any elements. On a lazy enumerable it walks every element to answer a yes/no question.

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-linq-count.cs
✗ Flagged
if (customer.Orders.Where(o => o.IsOverdue).Count() > 0)
{
    SendReminder(customer);
}
Why this trips

Deterministic line match: flags the method call `.Count()` with no arguments compared to 0 or 1 with any comparison operator, on either side.

csharp-linq-count.cs
✓ Clean
if (customer.Orders.Any(o => o.IsOverdue))
{
    SendReminder(customer);
}
What changed

Use `.Any()` for the non-empty check and `!.Any()` for the empty one. On a materialized collection, the `Count` or `Length` property is also fine.

Agents reach for the counting call by habit because it reads naturally, without considering what it costs on a query.

How this rule is justified.

Rule id

ai-slop/csharp-linq-count

Enforcing engine

ai-slop

Detector strategy

Deterministic line match: flags the method call `.Count()` with no arguments compared to 0 or 1 with any comparison operator, on either side.

Legitimate code that is NOT flagged

The `.Count` property of collections, `.Count(predicate)` calls, and comparisons against other numbers 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.