dotnet / roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.
https://josefpihrt.github.io/docs/roslynator
Other
3.09k stars 256 forks source link

RCS1077 for Select(x => x) #1490

Open bricelam opened 3 months ago

bricelam commented 3 months ago

RCS1077 should recommend removing .Select(x => x).

BenjaminBrienen commented 1 month ago

I'll take this

josefpihrt commented 1 month ago

@BenjaminBrienen This one can be tricky. In same cases it may be possible to just remove the call but there will be cases where the call is there intentionally to change return from whatever to IEnumerable<T>. And it may be not that easy to determine which case it is.

BenjaminBrienen commented 1 month ago

Good point.

BenjaminBrienen commented 1 month ago

@josefpihrt Could we have 2 code fixes? One that suggests to remove it and another one that suggests AsEnumerable()?

josefpihrt commented 1 month ago

Yes, there can be two fixes, but you still have an issue how to determine which one it is.

BenjaminBrienen commented 1 month ago

Yes, there can be two fixes, but you still have an issue how to determine which one it is.

I was imagining that the user would just pick one of the two suggestions.

Another strategy would be: We could always suggest AsEnumerable() and then also have a completely separate analyzer that handles unnecessary AsEnumerable() calls. That second analyzer would be the more difficult one to implement.

josefpihrt commented 1 month ago

We could always suggest AsEnumerable()

Actually, Select(x => x) is not semantically equivalent to .AsEnumerable(). Consider this example:

return list.Select(x => x);

If you change it to

return list.AsEnumerable();

Caller will have direct access to list which was not possible before.