dotnet/roslyn

IDE0059 incorrectly reported when a scoped variable is introduced.

オープン

#41,905 opened on 2020/02/24

 (5 件のコメント) (0 件のリアクション) (0 人の担当者)C# (4,257 件のフォーク)batch import
Area-IDEBugFeature - IDE0059help wanted

Repository metrics

Stars
 (20,414 個のスター)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

Version Used: 16.4.5

The following program triggers the bug. The code attempt += 1; is reported as unneeded which is incorrect.

using System;

class X
{
    public X() { }
    public X( int value ) => Value = value;
    public int Value { get; set; }
}

class Program
{
    static object Execute( X x ) => throw new NotImplementedException();


    static object Run()
    {
        var attempt = 1;

        while ( true )
        {
            try
            {
                var result = Execute( new X
                {
                    Value = 1
                } );

                return result;
            }
            catch when ( attempt <= 5 ) { }

            attempt += 1;
        }
    }
}

A few minor changes to the program eliminate the issue:

  1. Do not use property initializers for new X() - Execute( new X( 1 ) )
  2. Do not introduce the local result and instead just return Execute...

コントリビューターガイド