dotnet/roslyn

IDE0059 introduces unnecessary local

开放

#38,044 创建于 2019年8月16日

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

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

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();.

贡献者指南