KyleMit
KyleMit

Reputation: 29937

Why does Access to Modified Closure Warning Disappear When Enumerable Method called with ()

Resharper's Access to Modified Closure warnings are usually quite helpful. I was just noticing that when I call the Any method inside of a for each loop, I'll get a warning if I don't use the open and close parenthesis. As soon as I add (), the error goes away.

Does the error itself go away, or have I just accidentally tricked Resharper's static code analysis detection.

Dim groupExists as Boolean

For each oldPerson in oldData

    'access to modified closure warning on oldPerson.groupId
    groupExists = (From newPerson In newData 
                   Where newPerson.GroupId = oldPerson.groupId).Any

    'no closure problem reported
    groupExists = (From newPerson In newData 
                   Where newPerson.GroupId = oldPerson.groupId).Any()

Next

Of course, I can fix this by putting the following code inside the For Each loop and comparing the newPerson.GroupId to the locally declared variable.

'declare locally to avoid access to modified closure
Dim groupId as Integer = person.groupId

Upvotes: 1

Views: 87

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109119

I think this is a Resharper bug and I'd submit it there. There can't be a modified closure in a linq statement that's executing immediately, like Any does.

VB is not my 'native language', but afaik the parentheses shouldn't make any difference in VB.Net (as it did in VB6).

Upvotes: 3

Related Questions