Request for closure warning control attribute

The following code (contrived, but based on something we're seeing on a real project) gives an 'access to modified closure' warning on the 'item' variable in the WriteLine call.

This is not unreasonable, in that I don't expect R# to be able to look inside DoWork and conclude that because I don't save the lambda for future use, there's actually no problem.

However, I do know that there's no problem, and that DoWork will use-up and discard the lambda before returning.

What I think I want is something like a Jetbrains.Annotations.DoesNotSaveLambda attribute which I could apply to DoWork(), to indicate that the warning's unnecessary.

(I know that just adding a local copy of 'item' avoids the warning, but it's an ugly workaround, and I always feel compelled to write a comment to explain why the strange local variable is present)

Could we have something like this in R#6, please?   Or is there some hazard I'm missing here?

      void DoWork(Action<int> action)
        {
            action(0);
            action(1);
        }

        void MakeCalls()
        {
            int[] items = new[] { 1, 2, 3, 4 };
            foreach (int item in items)
            {
                DoWork((i) => Console.WriteLine(i + item));
            }
        }

2 comments
Comment actions Permalink

Hello Will,

Thank you for suggestion! There's a corresponding feature request in our
tracker: http://youtrack.jetbrains.net/issue/RSRP-122645 and you're welcome
to monitor its status. I've also added your comments to that issue and we
will certainly take your opinion into account as well.

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

The following code (contrived, but based on something we're seeing on
a real project) gives an 'access to modified closure' warning on the
'item' variable in the WriteLine call.

This is not unreasonable, in that I don't expect R# to be able to look
inside DoWork and conclude that because I don't save the lambda for
future use, there's actually no problem.

However, I do know that there's no problem, and that DoWork will
use-up and discard the lambda before returning.

What I think I want is something like a
Jetbrains.Annotations.DoesNotSaveLambda attribute which I could apply
to DoWork(), to indicate that the warning's unnecessary.

(I know that just adding a local copy of 'item' avoids the warning,
but it's an ugly workaround, and I always feel compelled to write a
comment to explain why the strange local variable is present)

Could we have something like this in R#6, please?   Or is there some
hazard I'm missing here?

void DoWork(Action<int> action)
{
action(0);
action(1);
}
void MakeCalls()
{
int[] items = new[] { 1, 2, 3, 4 };
foreach (int item in items)
{
DoWork( => Console.WriteLine(i + item));
}
}
---
Original message URL:
http://devnet.jetbrains.net/message/5276511#5276511



0
Comment actions Permalink

Andrey,

Thanks for this - it's good to see other people have had exactly the same idea as me already...

I've voted on it, but I guess it doesn't look very promising for 6.

Will

0

Please sign in to leave a comment.