dotnet/roslyn

IDE0059 introduces unnecessary local

オープン

#38,044 opened on 2019/08/16

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

Repository metrics

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

説明

Version Used: 3.3.0-beta3-19413-06+ac06df1bffb7b7fd0e5bf63cc91921af76b21e03

Steps to Reproduce:

using System;
using System.Runtime.InteropServices;

class Program
{
    static void Main() => Console.WriteLine(GetTopOfMemory());

    private static ulong GetTopOfMemory()
    {
        var info = new SYSTEM_INFO();
        GetSystemInfo(out info);
        return (ulong)info.lpMaximumApplicationAddress;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct SYSTEM_INFO
    {
        internal ushort wProcessorArchitecture;
        internal ushort wReserved;
        internal int dwPageSize;
        internal IntPtr lpMinimumApplicationAddress;
        internal IntPtr lpMaximumApplicationAddress;
        internal IntPtr dwActiveProcessorMask;
        internal int dwNumberOfProcessors;
        internal int dwProcessorType;
        internal int dwAllocationGranularity;
        internal short wProcessorLevel;
        internal short wProcessorRevision;
    }

    [DllImport("kernel32")]
    internal static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);
}

Execute IDE0059 on the var info = new SYSTEM_INFO(); line.

Expected Behavior: Either:

SYSTEM_INFO info;
GetSystemInfo(out info);
return (ulong)info.lpMaximumApplicationAddress;

or

GetSystemInfo(out SYSTEM_INFO info);
return (ulong)info.lpMaximumApplicationAddress;

or IDE0059 isn't offered at all.

Actual Behavior:

_ = new SYSTEM_INFO();
SYSTEM_INFO info;
GetSystemInfo(out info);
return (ulong)info.lpMaximumApplicationAddress;

which makes no sense, adding the _ = new SYSTEM_INFO();.

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