Split rule C# S2971 in multiple rules

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.

Hi Wilfried, thank you for your feedback, it helps us improve our products.
We are going to have a look at your proposal of splitting S2971, and come back to you with an answer as soon as possible.

Hello @wilfriedb,

Apologies for the late reply.

This is a very good suggestion indeed.
I’ve added an issue to our backlog to tackle when possible.

You can follow it here.

Best Regards
Mary

2 Likes