roslyn: IDE0028/IDE0090/IDE00300/IDE0305 do not catch some expressions

Analyzer

Diagnostic ID: IDE0028: Use collection initializers or expressions

IDE0090: Simplify new expression

IDE0300: Use collection expression for array

IDE0305: Use collection expression for fluent

Analyzer source

SDK: Built-in CA analyzers in .NET 8 SDK or later

Version: SDK 8.0.200

OR

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0

Describe the bug

I suppose this might not be considered a bug and could be more of a suggestion. Code like the following does not seem to trigger any of the collection expression/simplification rules.

public void CollectionAnalyzers()
{
	int[] array = new[] { 1, 2, 3 }; // IDE300
	List<int> list = new List<int>() { 1, 2, 3 }; // IDE0028, IDE0090

	List<int> list2 = new[] { 1, 2, 3 }.ToList(); // Should trigger IDE0028 and IDE300.
	List<int> list3 = new List<int> { 1, 2, 3 }.ToList(); // Should trigger IDE0028, IDE0090. Removing `ToList()` causes it to trigger both.
	int[] array2 = new List<int> { 1, 2, 3 }.ToArray(); // Should trigger IDE0300, IDE0090.

	// All 3 of the above lines should/could instead/in addition trigger a new rule; something like "Unnecessary collection copy".
	// The 1st and 3rd line obviously change the type, but there is no reason to be using the intermediate type in the first place, at least
	// in these cases. I suppose some types could have desired semantics, but I question how good that code would be anyway, considering that
	// the reason for doing it would probably not be obvious in most cases other than maybe something like `HashSet<T>`. The rule could "warn"
	// that it "Could change semantics" like IDE0028 sometimes does.

	// If a new rule isn't appropriate then maybe that should be configurable on IDE0305 so that it only triggers for initializations.
	// I don't really prefer the idea of IDE0305, but I WOULD use it if I could configure it to find redundant copying like this.

	List<int> list4 = new List<int>(array); // Should trigger IDE0028. Not passing in `array` causes it to trigger IDE0028.

	List<int> list5;

	list5 = new List<int>(array); // Should trigger IDE0028 and IDE0090. Not passing in `array` causes it to trigger IDE0028, but not IDE0090.
	list5 = new List<int>(array).ToList(); // Should trigger IDE0028, IDE0090 and IDE0305 and/or probably the new rule above. Not passing in `array` causes it to trigger IDE0305.

	object obj = new List<int> { 1, 2, 3 }.ToList(); // Should trigger IDE0305 and/or probably the new rule from above.
}

Steps To Reproduce

A reproduction project can be found here: https://github.com/bzd3y/ReproductionProject . Look at the Analyzers project Error List. It may help to filter the Error List to “Current Project”.

Expected behavior

The analyzers should be triggered for the above code. The expected behavior is documented there and in the reproduction project the code came from.

Actual behavior

Not all of the relevant analyzers seem like they are being triggered.

Additional context

One of the things going on seems to be that the ToList() and ToArray() for some reason cover up the other rules. But if IDE0305’s severity is set to none or silent then nothing is triggered and these lines are “lost”. This happens even when the expression type doesn’t change between the initialization, the method call or the assignment. I’m guessing that the analyzer just doesn’t go “deeper” into the RHS expression since it isn’t a direct initialization in these cases and is “wrapped” in a method call?

But the first assignment to list5 is still a direct initialization, I think, even though it isn’t at the point that list5 is declared. In the case of list5 I’m guessing the analyzer just doesn’t consider the type of list5 because it isn’t being declared there or something?   The code I am maintaining has areas that haven’t been touched in 10 years and these rules helped me modernize it and simplify a lot of old code but there were a few instances like some of the above that got missed and I found one serendipitously and then had to find the rest (maybe?) with Find and a regular expression. And yes, the original author actually did things like new List<T>().ToList() for some reason, and as a static readonly that never gets mutated so it probably just should have been an Array to begin with.

In closing, this seems like a combination of a bug in IDE0028/IDE0090 as well as maybe a reason to have either a new “Unnecessary collection copy” rule or a way to configure IDE0305 to only apply to initializations.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (10 by maintainers)

Most upvoted comments

We use ‘hiding’ in this solution many places to have a very specific meaning, and users who work on this project are likely to interpret this term in the context of this project even if that is not the intended meaning.

@sharwell Okay, I didn’t realize that, but it’s good to know. Thank you for explaining it.

@CyrusNajmabadi Sorry for the confusion.