Skip to content

IDE0059 incorrectly triggers when assigning to the indexer of a value type #77258

Closed
@tannergooding

Description

@tannergooding

Version Used: Compiler version: '4.14.0-1.25105.1 (842cb1bc)'. Language version: latest (13.0).

Steps to Reproduce:

  1. Create a new C# Console Application in VS 2022 v17.14.0 Preview 1 (it may also occur earlier, I didn't test)
  2. Update the code to the following:
int[] a = new int[5];
MyStruct m = new MyStruct(a);
m[0] = 1; // IDE0059: Unnecessary assignment of a value to 'm'
Console.WriteLine(a[0]);

struct MyStruct(int[] a)
{
    private int[] array = a;

    public int this[int index]
    {
        get => array[index];
        set => array[index] = value;
    }
}
  1. Observe that the line containing m[0] = 1; is flagged with ``

Diagnostic Id: IDE0059: Unnecessary assignment of a value to '...'

Expected Behavior:

The IDE warning should not trigger for method invocations or indexer usage, as these can trivially mutate indirect state. Such indirect side effects is the case in the example above where the contents of a are observably changed via the m[0] = 1;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions