-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Do not use OfType<T>() with impossible types #33770
Comments
Estimates:
|
Possibly at the same time, though via a different diagnostic ID: calling Enumerable.Range(1, 100).OfType<int>();
// @jnyrup points out that this is actually a non-null check,
// so isn't completely safe to remove
// myString.Split(",").OfType<object>(); |
I did a little prototype for fun over at dotnet/roslyn-analyzers#3608 |
@drewnoakes Note that for reference types this can be used as a less clear way of writing |
Approved as proposed Category: Reliability |
Warn if use of
OfType<T>()
would provably result in an empty sequence, because we can see that the input type in the sequence will never be the specified type.Category: Reliability
Suggested severity: Warning
UPDATE FROM roslyn-analyzer issue :
The following will always throw an InvalidCastException at runtime:
Perhaps worse, this will return an enumerable that fully enumerates the source and then returns no items silently:
Both of these situations are likely to be a mistake.
Ther is a PR out for review
The text was updated successfully, but these errors were encountered: