Skip to content

IDE0045 Bad suggestion results in CS8602 false-positive, revert results in bad IDE0045 suggestion #75200

Open

Description

Version Used:

.NET SDK:
 Version:           8.0.401
 Commit:            811edcc344
 Workload version:  8.0.400-manifests.b6724b7a
 MSBuild version:   17.11.4+37eb419ad

Steps to Reproduce:

Both these methods produce IDE0045:

public void M2(object[]? parent, int i, object value)
{
    if (parent == null) // IDE0045
    {
        throw new KeyNotFoundException();
    }
    parent[i] = value;
}

public void N(object[]? parent, int i, object value)
{
    if (parent is { }) // IDE0045
    {
        parent[i] = value;
    }
    else throw new KeyNotFoundException();
}

Which is simplified to

public void M(object[]? parent, int i, object value)
{
    parent[i] = parent is { } ? value : throw new KeyNotFoundException();
}

But, this emits analyzer warning CS8602, which on revert round-trips to IDE0045.

Sharplab

Diagnostic Id:

  • IDE0045: 'if' statement can be simplified
  • CS8602: Dereference of a possibly null reference.

Expected Behavior:
Either,

  • IDE0045 is not emitted in the above cases, where CS8602 can't validate nullability guard
  • CS8602 is not emitted in the above cases, where IDE0045 validly suggests case simplification.

Actual Behavior:
IDE0045 is suggested, but the suggestion results in CS8602, which when reverted suggests IDE0045 again. Repeat.

Disabling CS8602 or IDE0045 is not a valid solution, or viable long term workaround.

Relates: #63441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Area-IDEuntriagedIssues and PRs which have not yet been triaged by a lead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions