Reporter |
|
---|---|
Created | Apr 5, 2018 4:32:11 AM |
Updated | Apr 10, 2018 7:34:34 PM |
Resolved | Apr 10, 2018 7:34:34 PM |
Subsystem | Code Analysis - C# |
Assignee | Andrey Dyatlov (Andrey.Dyatlov) |
Priority | Normal |
State | Fixed |
Type | Bug |
Fix version | 2018.1 |
Affected versions | 2017.3.5 |
Fixed In Version ReSharper | Undefined |
VsVersion | VS 2017 RTM |
The problem manifests only inside local functions, functions defined inside of functions in C#7+
"Access to modified closure" appears when closing over a local variable binding (input formal parameter local var bindings), inside a local function, when there is a recursive call to itself (in tail position or otherwise, provided it's not provably branched away from). Note - this is in code where no mutation is taking place.
static void Main(string[] args)
{
void Thing(int val)
{
var foo = new Func<int>(() => val); // val gets the 'access to modified closure' warning
if (val <= 0)
{
return;
}
Thing(foo() - 1);
}
Thing(10);
Console.WriteLine("Done");
}
If the recursive call is removed, no more warning
void Thing(int val)
{
var foo = new Func<int>(() => val); // no warning here now.
if (val <= 0)
{
return;
}
// Thing(foo() - 1); // Take out the recursion, no more warning on the 'val'
}
Theoretically, derefencing the call back, alone, doesn't trigger the warning,
Expected: No "access to modified closure" warning on the 'val' variable when there is recursion.
Resharper Ultimate 2017.3.5
Devstudio 2017 v 15.6.2, .Net Framework 4.7.02556 (Using 4.7.1)