Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Analyzer: Warn when calling Enumerable.Cast<T>/OfType<T> with incompatible types #3608

Closed
fowl2 opened this issue May 7, 2020 · 4 comments
Labels
help wanted The issue is up-for-grabs, and can be claimed by commenting
Milestone

Comments

@fowl2
Copy link
Contributor

fowl2 commented May 7, 2020

The following will always throw an InvalidCastException at runtime:

new int[] { 1 }.Cast<string>().ToList();
new float[] { 1f }.Cast<double>().ToList();
new int[] { 1 }.Cast<long>().ToList();

Perhaps worse, this will return an enumerable that fully enumerates the source and then returns no items silently:

enumerableOfApple.OfType<Orange>()

Both of these situations are likely to be a mistake.

I'd never played with Roslyn before but I thought it'd be fun so I hacked together something. The rules seem quite complex so I have no confidence I got it right - or even if it's possible to spot 100% of cases - but it seems to be able to pick up the most obvious problems and I think that's still valuable.

Also, naming things and writing descriptive messages is really hard 😅.

An interesting thing I learnt was that enums can be cast to/from their underlying type and to other enums with the same underlying type this way, but numeric types can't be. I guess the numeric type conversions must be a language thing and so the compiled Cast<T>() can't know them.

Anyway I hope this is useful, even just for the idea.

Thanks

PS. It's amazing how good the getting started docs are here! (The comments in the code are a little sparse though ;))

@Evangelink
Copy link
Member

@fowl2 Do you want to give it a go and we will give some feedback or do you want me to take care of this one?

@Evangelink Evangelink added 2 - Ready Approved-Rule Indicates if the new proposed rule has been approved to move to implementation phase help wanted The issue is up-for-grabs, and can be claimed by commenting labels Oct 13, 2020
@fowl2
Copy link
Contributor Author

fowl2 commented Oct 14, 2020

I'll give it a go! Created draft PR #4328 to gather feedback.

@mavasani mavasani added this to the Unknown milestone Oct 22, 2020
@buyaa-n
Copy link
Contributor

buyaa-n commented Nov 6, 2020

The API Proposal dotnet/runtime#33770 ready for review

@carlossanlop carlossanlop added 4 - In Review and removed 2 - Ready Approved-Rule Indicates if the new proposed rule has been approved to move to implementation phase labels Nov 6, 2020
@carlossanlop
Copy link
Member

The API proposal has been approved, @fowl2

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

No branches or pull requests

6 participants