You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Applying the code fix suggested for S3267 can introduce C# warning CS8604 "Possible null reference argument for parameter..." if the transformation moves null checks into a LINQ expression and changes the nullability of expressions in the loop. In this situation, S3267 should not be raised.
S3267 is raised on the expression items in the foreach loop, and it offers 2 code fixes . If you apply the "Convert to LINQ (call form)" code fix, it changes the code to:
However, because the compiler doesn't know that the Where statement will remove all items where item.Prop is null, it now raises compiler warning CS8604 on the call to Bar
Note: It doesn't matter which of the two forms of code fix you choose - the same issue occurs.
Expected behavior
S3267 should not be raised if the transformation to LINQ would affect the nullability of any expressions within the loop.
Note: the bug is in the analyzer, not the code fix - in the general case for the possible loop bodies after the fix, it is not possible to write a LINQ version of this code that wouldn't cause the issue.
I would not consider this an FP from our side - IMO, the FP is that there is a null reference warning for item.Prop when a null check for it already exists after the code fix is applied.
I would suggest raising an issue for this on the Roslyn side.
These solutions are not perfect, but they are the best you can do now. There were some activities to address this problem, but all were rejected, and the only remaining initiative I'm aware of is dotnet/csharplang#3951. It is championed and was in the candidates set for C#10 but hasn't seen much traction recently. I don't expect any solution for this anytime soon.
Description
Applying the code fix suggested for S3267 can introduce C# warning CS8604 "Possible null reference argument for parameter..." if the transformation moves null checks into a LINQ expression and changes the nullability of expressions in the loop. In this situation, S3267 should not be raised.
Repro steps
Consider the method Foo below:
S3267 is raised on the expression
items
in the foreach loop, and it offers 2 code fixes . If you apply the "Convert to LINQ (call form)" code fix, it changes the code to:However, because the compiler doesn't know that the
Where
statement will remove all items whereitem.Prop
is null, it now raises compiler warning CS8604 on the call toBar
Note: It doesn't matter which of the two forms of code fix you choose - the same issue occurs.
Expected behavior
S3267 should not be raised if the transformation to LINQ would affect the nullability of any expressions within the loop.
Note: the bug is in the analyzer, not the code fix - in the general case for the possible loop bodies after the fix, it is not possible to write a LINQ version of this code that wouldn't cause the issue.
Actual behavior
S3267 is raised.
Known workarounds
Suppress S3267
Related information
The text was updated successfully, but these errors were encountered: