sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

NET-2391 Fix S1121 FP: Extract (null-forgiving) assignment from expression

Open DaleStan opened this issue 3 months ago • 4 comments

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.

DaleStan avatar Sep 26 '25 02:09 DaleStan

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

github-actions[bot] avatar Sep 26 '25 09:09 github-actions[bot]

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!

arjenkeijzer avatar Nov 12 '25 15:11 arjenkeijzer

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.

Tim-Pohlmann avatar Nov 19 '25 16:11 Tim-Pohlmann