-
Notifications
You must be signed in to change notification settings - Fork 32
AV1522: False positive on 'with' expressions (nondestructive mutation) #132
Copy link
Copy link
Closed
Description
Analyzer
Assign each variable in a separate statement (AV1522)
Repro steps
- Create a record type;
- Create an object of the record type;
- 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels