R# 7.1.2 bug?

Hello,

I have the following block of code.

          public override Stream GetResourceStream(string virtualPath)
          {
               IAssembly assembly = null;

               bool exists = TryGetResource(virtualPath, ref assembly);

               Stream resourceStream = null;

               if (exists)
               {
                    resourceStream = assembly.GetManifestResourceStream(virtualPath);

                    foreach (var rewriter in _rewriters)
                    {
                         if (resourceStream != null && rewriter.CanRewrite(virtualPath))
                         {
                              resourceStream = rewriter.RewriteStream(virtualPath, resourceStream);
                         }
                    }
               }

               return resourceStream;
          }


R# suggests me to change it into this.

                    foreach (var rewriter in _rewriters.Where(rewriter => resourceStream != null && rewriter.CanRewrite(virtualPath)))
                    {
                         resourceStream = rewriter.RewriteStream(virtualPath, resourceStream);
                    }


Now, it complains that I'm accessing a modified clousure and suggests me to change it into this.

          public override Stream GetResourceStream(string virtualPath)
          {
               IAssembly assembly = null;

               bool exists = TryGetResource(virtualPath, ref assembly);

               Stream[] resourceStream = { null };

               if (exists)
               {
                    resourceStream[0] = assembly.GetManifestResourceStream(virtualPath);

                    foreach (var rewriter in _rewriters.Where(rewriter => resourceStream[0] != null && rewriter.CanRewrite(virtualPath)))
                    {
                         resourceStream[0] = rewriter.RewriteStream(virtualPath, resourceStream[0]);
                    }
               }

               return resourceStream[0];
          }


Now, due to the change that was made to C# 5.0 I believe that it's redundant.

Going farther I think that I can change it into this.

resourceStream = _rewriters.Where(resourceRewriter => resourceStream != null && resourceRewriter.CanRewrite(virtualPath))
                                               .Aggregate(resourceStream, (current, resourceRewriter) => resourceRewriter.RewriteStream(virtualPath, current));


I've tested this and my tests show that it's working, so it's either intentional or a bug.

1 comment
Comment actions Permalink

Any news? I'd like to know whether it's a bug...

0

Please sign in to leave a comment.