Skip to content

CA1508 is triggered despite value being set #51179

@ericstj

Description

@ericstj

Version Used:
.NET SDK:
Version: 10.0.100-rc.2.25462.101
Commit: 26861c107e
Workload version: 10.0.100-manifests.663d2eb1
MSBuild version: 17.15.0-preview-25462-101+26861c107

Steps to Reproduce:

  1. Compile attached sample:
string? testValue = null;

var class2 = new Class2
{
    action = (value) => testValue = value
};

class2.Indirection();

if (testValue != null)
{
    // Do something
    Console.WriteLine(testValue);
}

public class Class2()
{
    public Action<string>? action { get; init; }
    public void Indirection()
    {
        if (action != null)
            action("Hello World!");
    }
}

repro_CA1508.zip

Diagnostic Id:
CA1508: Avoid dead conditional code

Expected Behavior:
Compile without warning

Actual Behavior:
Warning

warning CA1508: 'testValue != null' is always 'false'. Remove or refactor the condition(s) to avoid dead code. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1508)

This warning is clearly false, since running the application shows that testValue is not null. The analyzer is not able to see enough about the possibility of modification to testValue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions