NET-2391 Fix S1121 FP: Extract (null-forgiving) assignment from expression
Description
SonarQube should not recommend reverting IDE0031 after the fix is applied to the following code. (using <LangVersion>preview</LangVersion>, which will become <LangVersion>14</LangVersion> in a couple weeks.)
class Foo {
Foo? child;
public void DoSomething(Foo? other) {
if (other != null) // IDE0031: Simplify null check
other.child = this;
}
}
The automatic fix for IDE0031 changes this code to the reproducer below.
Reproducer
class Foo {
Foo? child;
public void DoSomething(Foo? other) {
other?.child = this; // Unexpected S1121
}
}
Expected behavior
S1121 should not be raised on null-forgiving assignments.
Product and Version
SonarQube 8.26.0.14275 on Visual Studio 17.14.15 and Windows 11 24H2.
Hi,
Thank you for reporting this. We do not support the preview of C# 14 yet, it will come later this year.
Internal ticket NET-2391
Since we moved to NET 10 today, we needed to set the severity of S1121 to suggestion. I hope that this issue will be fixed soon!
It is on our radar. It's definitely one of the more severe C# 14 surprises. We'll post an update here, when it is fixed.