Quantcast
Channel: YouTrackReSharper (RSRP) - Bug and Issue Tracker
Viewing all articles
Browse latest Browse all 106942

RSRP-288411: Cast() when converting to LINQ expressions

$
0
0
Reporter Mauricio Scheffer (mausch) 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:

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);

Viewing all articles
Browse latest Browse all 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>