Skip to content

Code fix for CA1860 introduces a breaking change in a case with null propagation #7797

@aleomel-dfds

Description

@aleomel-dfds

The following code snippet triggers CA1860 analyzer diagnostic and the code fix is enabled:

record Foo(ICollection<string> List);
Foo foo = null;
if (foo?.List.Any() == true) // CA1860 with code fix
{
  Console.WriteLine("oops");
}
// does not print anything

The code fix changes the snippet to this and breaks the existing behaviour:

record Foo(ICollection<string> List);
Foo foo = null;
if (foo?.List.Count != 0 == true)
{
  Console.WriteLine("oops");
}
// prints "oops"

The code fix should not be offered in case above, as it introduces a breaking change. Interestingly, the code fix is already not provided when the null propagation is used on the collection variable directly:

ICollection<string> list = null;
if (list?.Any() == true) // CA1860 with no code fix
{
  Console.WriteLine("oops");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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