Skip to content

AV1522: False positive on 'with' expressions (nondestructive mutation) #132

@ghost

Description

Analyzer

Assign each variable in a separate statement (AV1522)

Repro steps

  1. Create a record type;
  2. Create an object of the record type;
  3. Using a 'with' expression (aka nondestructive mutation) make a new object of the record, modifying at least one property;

Expected behavior

No warning, because the whole expression is a single assignment to the newly created object.

Actual behavior

A warning is reported that the Class.Property and variable are both assigned in a single statement.

Example from the linked page

public record Person(string FirstName, string LastName);

public static void Main()
{
    Person person1 = new("Nancy", "Davolio");
    Console.WriteLine(person1);
    // output: Person { FirstName = Nancy, LastName = Davolio }

    Person person2 = person1 with { FirstName = "John" }; // warning here
    Console.WriteLine(person2);
    // output: Person { FirstName = John, LastName = Davolio }
}

Warning at 'warning here' states: 'person2' and 'Person.FirstName' are assigned in a single statement

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions