dotnet / roslyn-analyzers

MIT License
1.56k stars 462 forks source link

IDE0305 fix description/make configurable #7259

Open bzd3y opened 3 months ago

bzd3y commented 3 months ago

Analyzer

Diagnostic ID: IDE0305

Describe the improvement

The documentation for this rule says :

This rule flags places where a collection is built in a fluent manner, that is, where methods like Add(), AddRange(), AsSpan(), ToList(), and ToArray() are chained.

But the rule is triggered when there is no fluent/chaining of methods.

Describe suggestions on how to achieve the rule

The documentation/description should probably be changed to indicate that it is just triggered by using any methods that can be chained. (But why are these methods even there or can be used fluently if we aren't supposed to use them that way?)

Or, ideally, I think that it would help if it was just made to be configurable with an option to trigger like it does now or to only trigger for actual fluent code with chained methods. Right now if it sees something like someList.ToArray() it will suggest something like [.. someList] even though that isn't really fluent or any chaining. Sure, that is fewer characters to type, if you don't count Intellisense helping you. But it isn't nearly as readable. Now, if it does encounter some chain of multiple methods then it might make more sense to refactor that to use collection expressions.

So it would be nice to have something like:

dotnet_style_prefer_collection_expression = when_multiple_methods

Honestly, ideally it might just take an integer and if the chain is that long or longer it triggers. So something like one/both of:

dotnet_style_prefer_collection_expression_chain_length = 2
dotnet_style.IDE0305.chain_length = 2