Roslyn: Suggestion: Analyzer to suggest fix for working with non-generic collections

Created on 5 May 2018  路  9Comments  路  Source: dotnet/roslyn

There are some commonly-used APIs from .NET 1.x that expose non-generic collections/enumerables. Developers can be surprised when enumerating over these collections that the inferred type is System.Object and that they have to rely on C# 1.x-era implicit upcasting to enumerate over the values, which can fail at runtime if they get the type wrong. I think that an analyzer could be built with knowledge of these common APIs that would identify when these collections are enumerated and offer fixes to the proper type as well as error diagnostics if the type is incorrect:

var rgx = new Regex("Some Regex");
foreach (var m in rgx.Matches("Some Text"))  // suggests fixing var to Match
{
}
var rgx = new Regex("Some Regex");
foreach (Foo f in rgx.Matches("Some Text")) // error casting Match to Foo
{
}
Area-Analyzers

Most helpful comment

@alrz Not without breaking existing code. In .Net Core 2.0, those types do implement IEnumerable<T> (which means that LINQ works), but foreach still uses the old GetEnumerator() method, which returns non-generic IEnumerator.

All 9 comments

Would it be possible to just fix the APIs? (i.e. add generic enumerator)

@alrz Not without breaking existing code. In .Net Core 2.0, those types do implement IEnumerable<T> (which means that LINQ works), but foreach still uses the old GetEnumerator() method, which returns non-generic IEnumerator.

This is good, but what about hundereds of collection classes that can be removed and replaced by List< T > , such as List< Match > in Regex Class?.. The benifits are bigger than infering the var type.
I think this worth making a breaking change and give old apps some options to recover from it, like adding an analyzer to help old apps to find and fix broken code.

@MohammadHamdyGhanem Would you be willing to help make this happen?

@CyrusNajmabadi

Would you be willing to help make this happen?

With pleasure. I just suggested to make these changes as Nugets, so anyone wants to use the modern collection classes can download them. Is this possible?
I have troubles dealing with github, besides it seems I need to download all the source code of the CoreFX to modify such classes. I faild to modify the StringBuilder Class alone, becuase its dependencies. I'm not familiar with the process of open source yet.

The request is to work on this issue. i.e. "Suggestion: Analyzer to suggest fix for working with non-generic collections"

it would be worthwhile putting your energy into solutions that would be accepted.

it would be worthwhile putting your energy into solutions that would be accepted.

I'm 41 years old now. No much energy left! :D
I intend to participat in the near futue, but this need some reading to fully understand this open source environment (and how to write an analizer), and I have plenty of topics to read and write about for the next few months.
I think I will start by implementing my idea about LinQ To Regex which excits me the most.
Thanks.

foreach (Foo f in rgx.Matches("Some Text")) // error casting Match to Foo

This one is basically my suggestion at dotnet/roslyn-analyzers#1494.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OndrejPetrzilka picture OndrejPetrzilka  路  3Comments

codingonHP picture codingonHP  路  3Comments

NikChao picture NikChao  路  3Comments

marler8997 picture marler8997  路  3Comments

orthoxerox picture orthoxerox  路  3Comments