dotnet/roslyn

IDE0059 introduces unnecessary local

Aperta

#38.044 aperta il 16 ago 2019

 (3 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-IDEBugFeature - IDE0059IDE-CodeStylehelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

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

Guida contributor