dotnet/roslyn

IDE0059 incorrectly suggests removal of used variable

开放

#64,044 创建于 2022年9月15日

 (2 条评论) (1 个反应) (0 位负责人)C# (4,257 个派生)batch import
Area-IDEBugFeature - IDE0059help wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南