Count() is compared with zero where Any() would stop at the first element

tx0/csharp:count-instead-of-anyrecommendedwarningperformance/algorithmicdepth fastperformance

What it finds

items.Count() > 0 enumerates the whole sequence to count it, only to learn whether it is empty. items.Any() stops at the first element. Collections with a Count property make the call cheap, but the property says the same more directly.

From the rule's source, count_instead_of_any.rs.

Example from the rule's tests
using System.Collections.Generic;using System.Linq; class Checks{    bool Run(IEnumerable<int> items)    {        if (items.Count() > 0)tx0 reports: items.Count() > 0 counts every element to test for emptiness        {            return items.Count() == 0;tx0 reports: items.Count() == 0 counts every element to test for emptiness        }        return items.Any() || items.Count() > 2;    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:count-instead-of-any" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:count-instead-of-any] reason="..."

tx0 explain tx0/csharp:count-instead-of-any shows the same in the terminal. Found a false positive? Report it.