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

RSRP-148744: "Access to modified closure" quick fix breaks code logic

$
0
0
Reporter Boris Kartamyshev (boriskv) Boris Kartamyshev (boriskv)
Created Jan 19, 2010 7:29:20 PM
Updated Apr 6, 2018 6:51:21 PM
Subsystem Quick Fixes
Assignee Alisa Afonina (alisa.afonina)
Priority Major
State Submitted
Type Bug
Fix version 2018.2
Affected versions 2018.1
Fixed In Version ReSharper Undefined
VsVersion All Versions
Consider following test code (simplified version of original code):

...
    List<int> list = new List<int>();
    for (int i = 0; i < list.Count; i++)
    {
        InvokeAction(() => list.RemoveAt(i--));
    }
...

protected void InvokeAction(Action method)
{
    if (InvokeRequired)
        Invoke(method);
    else
        method();
}

 in list.RemoveAt(*+i+*--)
); i is highlighted with wavy line and warning "Access to modified closure" is shown.

If copy to local variable fix is chosen code is transformed to:

List<int> list = new List<int>();
for (int i = 0; i < list.Count; i++)
{
    int i1 = i;
    InvokeAction(() => list.RemoveAt(i1--));
}

which breaks original logic. I believe fixed code should look like following snippet:

List<int> list = new List<int>();
for (int i = 0; i < list.Count; i++)
{
    int i1 = i--;
    InvokeAction(() => list.RemoveAt(i1));
}

Viewing all articles
Browse latest Browse all 106942

Trending Articles



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