Skip to content

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

@DaleStan

Description

@DaleStan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions