codeanalyzers icon indicating copy to clipboard operation
codeanalyzers copied to clipboard

U2U1212 incorrectly suggests extracting Guid.NewGuid() out of a LINQ expression.

Open r-ryabikov opened this issue 1 year ago • 0 comments

Consider the following LINQ expression:

noGuidObjects.Select(nio => new IdObject { Id = Guid.NewGuid(), Data = nio.Data });

Version 3.8.13 of the extension suggests that I "Consider extracting the subexpression Id = Guid.NewGuid() from the lambda expression to avoid repeated calculation", when the repeated calculation of a unique GUID for each item is, in fact, the point of the expression. I'm not sure if there is a reliable way to tell if an assignment in the subexpression is supposed to return a different value after every call, and therefore has to be re-evaluated at each iteration. But it feels like assuming this true for all parameterless method calls would be correct in more cases than otherwise.

Either way, thank you for your time creating and maintaining this extension.

r-ryabikov avatar Jun 20 '24 09:06 r-ryabikov