dotnet/roslyn

IDE0059 incorrectly suggests removal of used variable

Open

#64,044 opened on Sep 15, 2022

View on GitHub
 (2 comments) (1 reaction) (0 assignees)C# (4,257 forks)batch import
Area-IDEBugFeature - IDE0059help wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

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