Skip to content

IDE0031 false positive when #if DEBUG is used #65880

@elachlan

Description

@elachlan

Analyzer

Diagnostic ID: [IDE0031](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0031: Use null propagation

Analyzer source

.NET7

Describe the bug

Stumbled upon in dotnet/winforms#7902

The analyzer doesn't take into account #if DEBUG statements. Inside visual studio it doesn't suggest a code fix. But in CI build it fails.

public void Dispose()
{
    if (_controlToLayout is not null)
    {
        _controlToLayout.ResumeLayout(_resumeLayout);

#if DEBUG
        Debug.Assert(_controlToLayout.LayoutSuspendCount == _layoutSuspendCount, "Suspend/Resume layout mismatch!");
#endif
    }
}

Steps To Reproduce

Enable the analyzer as a warning or error and build a solution with a null check and a #if DEBUG statement.

Expected behavior

No build failure.

Actual behavior

Build Failure.

Additional context

This is blocking winforms uptake of the rule.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions