Skip to content

CA1861 code fix doesn't use unique name #6981

@meziantou

Description

@meziantou

Analyzer

Diagnostic ID: CA1861: Extract to static readonly field

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 8.0.0-rc2

Describe the bug

CA1861 code fix creates a new field that can have the same name as a local variable

image

Steps To Reproduce

Original code:

class Sample
{
    void A(char separator)
    {
        "".Split(new char[] { 'a', 'b' });
    }
}

After code fix:

class Sample
{
    internal static readonly char[] separator = new char[] { 'a', 'b' };

    void A(char separator)
    {
        // "separator" is the local parameter instead of the static field
        "".Split(separator);
    }
}

Expected behavior

Generate a unique name

Actual behavior

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CodeFixBug in code fixerBugThe product is not behaving according to its current intended designgood first issuehelp wantedThe issue is up-for-grabs, and can be claimed by commenting

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions