Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1854 Fix breaks existing code if the identifier 'value' is already used. #6287

Closed
N-Olbert opened this issue Nov 24, 2022 · 1 comment
Closed
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design Resolution-Duplicate

Comments

@N-Olbert
Copy link

Analyzer

Diagnostic ID: CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method

Analyzer source

SDK: Built-in CA analyzers in .NET 7 SDK

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 7.0.0-preview1.22518.1 (Latest preview, but also shipped with .NET 7)

Describe the bug

If a variable named 'value' exists within the current scope, the fix of this analyzer breaks existing code.
This is not an issue for every-day-development (everyone should be smart enough to figure out an appropriate different name himself) but it is a bit of a pity for huge (legacy) code bases that should be updated ("Fix all in project/solution").

Steps To Reproduce

Note: I agree that this is bad naming in general, though 👎

private Dictionary<string, string> foo;

public string GetFoo(string fooName)
{
    var value = "Default";
    if (this.foo.ContainsKey(fooName))
    {
        var valueString = this.foo[fooName];
        if (!string.IsNullOrEmpty(valueString))
        {
            value = Bar(valueString);
        }
    }

    return value;
}

Expected behavior

private Dictionary<string, string> foo;

public string GetFoo(string fooName)
{
    var value = "Default";
    if (this.foo.TryGetValue(fooName, out var value1)) // Note: 'value1 instead of 'value'
    {
        var valueString = value1;
        if (!string.IsNullOrEmpty(valueString))
        {
            value = Bar(valueString);
        }
    }

    return value;
}

Actual behavior

private Dictionary<string, string> foo;

public string GetFoo(string fooName)
{
    var value = "Default";
    if (this.foo.TryGetValue(fooName, out var value)) // Boom
    {
        var valueString = value;
        if (!string.IsNullOrEmpty(valueString))
        {
            value = Bar(valueString);
        }
    }

    return value;
}

This won't compile-

Idea to fix

How about appending a number to the generated 'value' identifier, if 'value' is already used within the current scope?

@Youssef1313
Copy link
Member

Duplicate of #6022

@Youssef1313 Youssef1313 marked this as a duplicate of #6022 Dec 8, 2022
@Youssef1313 Youssef1313 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 8, 2022
@Youssef1313 Youssef1313 added Bug The product is not behaving according to its current intended design Resolution-Duplicate Area-Microsoft.CodeAnalysis.NetAnalyzers labels Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design Resolution-Duplicate
Projects
None yet
Development

No branches or pull requests

2 participants