dotnet/roslyn

IDE0059 incorrectly suggests removal of used variable

Open

#64.044 geöffnet am 15. Sept. 2022

Auf GitHub ansehen
 (2 Kommentare) (1 Reaktion) (0 zugewiesene Personen)C# (4.257 Forks)batch import
Area-IDEBugFeature - IDE0059help wanted

Repository-Metriken

Stars
 (20.414 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

Version Used: Microsoft Visual Studio Enterprise 2022 (64-bit) - Current Version 17.3.3, TargetFramework net6.0

Steps to Reproduce:

int count;
try
{
    if (0 == 1) { } // comment out to "fix" suggestion
    Console.WriteLine(); // comment out to "fix" suggestion
}
finally
{
    count = 3;
}

if (count == 0) { }

Expected Behavior: Do not delete my variable assignment.

Actual Behavior: Wants to break my code. If I follow the suggestion it removes the variable. It can alter the code semantics by deleting necessary assignments. In my example the compiler will error because count was not assigned but it could be more subtle. If you comment out either statement in the try block, the suggestion will disappear.

Contributor Guide