Skip to content

Use Range Operator should check for writable indexer #43202

@sharwell

Description

@sharwell

Version Used: 16.6 Preview 2

Steps to Reproduce:

struct S
{
  public ref S Slice(int start, int length) => throw null;
  public int Length { get; }
  public S this[System.Range r] { get => default; }
}

class C
{
    void Goo(S s)
    {
        s.Slice(1, s.Length - 2) = default;
    }
}

Expected Behavior:

No code fix is offered to convert the Slice call to a range operator.

Actual Behavior:

The call is converted to this:

s[1..^1] = default;

The result does not compile because the indexer is read only.

error CS0200: Property or indexer 'S.this[Range]' cannot be assigned to -- it is read only

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugIDE-CodeStyleBuilt-in analyzers, fixes, and refactoringshelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions