dotnet/roslyn

IDE0059 introduces unnecessary local

Open

#38,044 opened on Aug 16, 2019

View on GitHub
 (3 comments) (0 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-IDEBugFeature - IDE0059IDE-CodeStylehelp wanted

Description

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

Contributor guide