Rule S2971 is actually multiple rules folded into one. According to S2971 (sonarsource.github.io) these are the rules:
1 Use OfType
instead of using Select
with as
to type cast elements and then null-checking in a query expression to choose elements based on type.
2 Use OfType
instead of using Where
and the is
operator, followed by a cast in a Select
3 Use an expression in Any
instead of Where(element => [expression]).Any()
.
4 Use Count
instead of Count()
when it’s available.
5 Don’t call ToArray()
or ToList()
in the middle of a query chain.
It would be better to split them out into four or five seperate rules so they can be judged independently. For example, I find rule 3 not useful at all, it lessens the readability instead of improving. And even more, the suggested code is actually slower. Also, it is more of a style rule. I don’t think Sonar rules should be about coding style.
But with disabling rule S2971 I would miss out on the other four, which are actually useful. Rule 1 and 2 are similar and could stay combined. Therefore I suggest to split this rule into four separate rules.