Reporter | Mauricio Scheffer (mausch) |
---|---|
Created | Feb 20, 2012 6:49:16 AM |
Updated | Feb 20, 2012 6:49:16 AM |
Priority | Normal |
Type | Unspecified |
Fix versions | No Fix versions |
State | Submitted |
Assignee | Unassigned |
Subsystem | No subsystem |
Affected versions | 6.1.1 |
Fixed in build | No Fixed in build |
Consider this simple (a bit silly) code:
ReSharper 6.1.42.60 suggests to convert the foreach to a LINQ expression. This transforms the code into:
Here Cast<Stream>() is unnecessary in .NET 4 (covariance), but more importantly, it should never suggest to apply Cast<T>() on an IEnumerable<T>(). Cast<T>() is not type-safe, it should only be suggested for non-generic IEnumerables. If I later change the array to something like:
ReSharper shows a "suspicious cast" warning, but there is of course no warning or error from the compiler.
If anything, ReSharper should transform to a Select<T>() with an explicit cast, e.g. a.Select(x => (Stream)x);
IEnumerable<Stream> Yield() { var a = new[] {new MemoryStream()}; foreach (var i in a) yield return i; }
ReSharper 6.1.42.60 suggests to convert the foreach to a LINQ expression. This transforms the code into:
IEnumerable<Stream> Yield() { var a = new[] {new MemoryStream()}; return a.Cast<Stream>(); }
Here Cast<Stream>() is unnecessary in .NET 4 (covariance), but more importantly, it should never suggest to apply Cast<T>() on an IEnumerable<T>(). Cast<T>() is not type-safe, it should only be suggested for non-generic IEnumerables. If I later change the array to something like:
var a = new[] {new StringBuilder()};
ReSharper shows a "suspicious cast" warning, but there is of course no warning or error from the compiler.
If anything, ReSharper should transform to a Select<T>() with an explicit cast, e.g. a.Select(x => (Stream)x);