ronantakizawa/codereview-bench huggingface dataset:
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs (C#)

Code before:

            if (command is not null)
            {
                return new FeedbackItem(
                    StringUtil.Format(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory, target),
                    new List<string> { localTarget });
            }

            // Check fuzzy matching command names.
            var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace);
            var results = pwsh.AddCommand("Get-Command")
                    .AddParameter("UseFuzzyMatching")
                    .AddParameter("FuzzyMinimumDistance", 1)
                    .AddParameter("Name", target)
                .AddCommand("Select-Object")
                    .AddParameter("First", 5)
                    .AddParameter("Unique")
                    .AddParameter("ExpandProperty", "Name")
                .Invoke<string>();

            if (results.Count > 0)
            {
                return new FeedbackItem(
                    SuggestionStrings.Suggestion_CommandNotFound,
                    new List<string>(results),
                    FeedbackDisplayLayout.Landscape);
            }

            return null;
        }
    }
}


Review comment (suggestion):
It feels to me the number of results is not very useful data for us to collect. Maybe we just collect whether this feature gets used by sending the telemetry here instead? https://github.com/PowerShell/PowerShell/blob/b428e1a393242ce15e54f24c6fcd9d8bae97ada2/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs#L2548

Code after:
                CommandTypes.Application | CommandTypes.ExternalScript);

            if (command is not null)
            {
                return new FeedbackItem(
                    StringUtil.Format(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory, target),
                    new List<string> { localTarget });
            }

            // Check fuzzy matching command names.
            var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace);
            var results = pwsh.AddCommand("Get-Command")
                    .AddParameter("UseFuzzyMatching")
                    .AddParameter("FuzzyMinimumDistance", 1)
                    .AddParameter("Name", target)
                .AddCommand("Select-Object")
                    .AddParameter("First", 5)
                    .AddParameter("Unique")
                    .AddParameter("ExpandProperty", "Name")
                .Invoke<string>();

			if (results.Count > 0)
			{
				ApplicationInsightsTelemetry.SendUseTelemetry("FuzzyMatching", "CommandNotFound");
				return new FeedbackItem(
					SuggestionStrings.Suggestion_CommandNotFound,
					new List<string>(results),
					FeedbackDisplayLayout.Landscape);
			}

            return null;
        }
    }
}
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs (C#)

Code before:
            var command = invocation.GetCommand(
                localTarget,
                CommandTypes.Application | CommandTypes.ExternalScript);

            if (command is not null)
            {
                return new FeedbackItem(
                    StringUtil.Format(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory, target),
                    new List<string> { localTarget });
            }

            // Check fuzzy matching command names.
            var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace);
            var results = pwsh.AddCommand("Get-Command")
                    .AddParameter("UseFuzzyMatching")
                    .AddParameter("FuzzyMinimumDistance", 1)
                    .AddParameter("Name", target)
                .AddCommand("Select-Object")
                    .AddParameter("First", 5)
                    .AddParameter("Unique")
                    .AddParameter("ExpandProperty", "Name")
                .Invoke<string>();

			if (results.Count > 0)
			{
				ApplicationInsightsTelemetry.SendUseTelemetry("PSCommandNotFoundSuggestion", "CommandNotFound");
				return new FeedbackItem(
					SuggestionStrings.Suggestion_CommandNotFound,
					new List<string>(results),
					FeedbackDisplayLayout.Landscape);
			}

            return null;
        }
    }
}


Review comment (suggestion):
Now that there are multiple "command not found" feedback providers, it's better to use a more concrete name for this one, such as
```suggestion
				ApplicationInsightsTelemetry.SendUseTelemetry("FuzzyMatching", "CommandNotFound");
```

Code after:
            var command = invocation.GetCommand(
                localTarget,
                CommandTypes.Application | CommandTypes.ExternalScript);

            if (command is not null)
            {
                return new FeedbackItem(
                    StringUtil.Format(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory, target),
                    new List<string> { localTarget });
            }

            // Check fuzzy matching command names.
            var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace);
            var results = pwsh.AddCommand("Get-Command")
                    .AddParameter("UseFuzzyMatching")
                    .AddParameter("FuzzyMinimumDistance", 1)
                    .AddParameter("Name", target)
                .AddCommand("Select-Object")
                    .AddParameter("First", 5)
                    .AddParameter("Unique")
                    .AddParameter("ExpandProperty", "Name")
                .Invoke<string>();

			if (results.Count > 0)
			{
				ApplicationInsightsTelemetry.SendUseTelemetry("FuzzyMatching", "CommandNotFound");
				return new FeedbackItem(
					SuggestionStrings.Suggestion_CommandNotFound,
					new List<string>(results),
					FeedbackDisplayLayout.Landscape);
			}

            return null;
        }
    }
}
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Subsystem/SubsystemInfo.cs (C#)

Code before:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Internal;
using Microsoft.ApplicationInsights;
using Microsoft.PowerShell.Telemetry;

namespace System.Management.Automation.Subsystem
{
    /// <summary>
    /// Class used to represent the metadata and state of a subsystem.
    /// </summary>
    public abstract class SubsystemInfo
    {
        #region "Metadata of a Subsystem (public)"

        /// <summary>
        /// Gets the kind of a concrete subsystem.
        /// </summary>
        public SubsystemKind Kind { get; }

        /// <summary>
        /// Gets the type of a concrete subsystem.
        /// </summary>
        public Type SubsystemType { get; }

        /// <summary>
        /// Gets a value indicating whether the subsystem allows to unregister an implementation.
        /// </summary>
        public bool AllowUnregistration { get; private set; }

Review comment (suggestion):
Is this using directive doesn't seem to be needed.

```suggestion
```

Code after:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Internal;
using Microsoft.PowerShell.Telemetry;

namespace System.Management.Automation.Subsystem
{
    /// <summary>
    /// Class used to represent the metadata and state of a subsystem.
    /// </summary>
    public abstract class SubsystemInfo
    {
        #region "Metadata of a Subsystem (public)"

        /// <summary>
        /// Gets the kind of a concrete subsystem.
        /// </summary>
        public SubsystemKind Kind { get; }

        /// <summary>
        /// Gets the type of a concrete subsystem.
        /// </summary>
        public Type SubsystemType { get; }

        /// <summary>
        /// Gets a value indicating whether the subsystem allows to unregister an implementation.
        /// </summary>
        public bool AllowUnregistration { get; private set; }
Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Management/commands/management/JobProcessCollection.cs (C#)

Code before:
        do
        {
            Interop.Windows.GetQueuedCompletionStatus(
                _completionPort,
                INFINITE,
                out completionCode);
        }
        while (completionCode != Interop.Windows.JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO);
        cancellationToken.ThrowIfCancellationRequested();
    }

    [MemberNotNullWhen(true, [nameof(_jobObject), nameof(_completionPort)])]
    private bool InitializeJob()
    {
        if (_initStatus.HasValue)
        {
            return _initStatus.Value;
        }

        if (_jobObject is null)
        {
            _jobObject = Interop.Windows.CreateJobObject();
            if (_jobObject.IsInvalid)
            {
                _initStatus = false;
                return false;
            }
        }

        if (_completionPort is null)
        {
            _completionPort = Interop.Windows.CreateIoCompletionPort();

            if (_completionPort.IsInvalid)
            {
                _initStatus = false;
                return false;
            }
        }

        _initStatus = Interop.Windows.SetInformationJobObject(
            _jobObject,
            _completionPort);

        return _initStatus.Value;
    }

    public void Dispose()
    {
        _jobObject?.Dispose();
        _completionPort?.Dispose();

Review comment (suggestion):
```suggestion
            _jobObject = Interop.Windows.CreateJobObject();
            if (_jobObject.IsInvalid)
            {
                _initStatus = false;
                jobObject.Dispose();
                jobObject = null;
                return false;
```

Code after:
        {
            Interop.Windows.GetQueuedCompletionStatus(
                _completionPort,
                Interop.Windows.INFINITE,
                out completionCode);
        }
        while (completionCode != Interop.Windows.JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO);
        cancellationToken.ThrowIfCancellationRequested();
    }

    [MemberNotNullWhen(true, [nameof(_jobObject), nameof(_completionPort)])]
    private bool InitializeJob()
    {
        if (_initStatus.HasValue)
        {
            return _initStatus.Value;
        }

        if (_jobObject is null)
        {
            _jobObject = Interop.Windows.CreateJobObject();
            if (_jobObject.IsInvalid)
            {
                _initStatus = false;
                _jobObject.Dispose();
                _jobObject = null;
                return false;
            }
        }

        if (_completionPort is null)
        {
            _completionPort = Interop.Windows.CreateIoCompletionPort();
            if (_completionPort.IsInvalid)
            {
                _initStatus = false;
                _completionPort.Dispose();
                _completionPort = null;
                return false;
            }
        }

        _initStatus = Interop.Windows.SetInformationJobObject(
            _jobObject,
            _completionPort);

        return _initStatus.Value;
    }

    public void Dispose()
    {
Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Management/commands/management/JobProcessCollection.cs (C#)

Code before:
    private bool InitializeJob()
    {
        if (_initStatus.HasValue)
        {
            return _initStatus.Value;
        }

        if (_jobObject is null)
        {
            _jobObject = Interop.Windows.CreateJobObject();
            if (_jobObject.IsInvalid)
            {
                _initStatus = false;
                return false;
            }
        }

        if (_completionPort is null)
        {
            _completionPort = Interop.Windows.CreateIoCompletionPort();

            if (_completionPort.IsInvalid)
            {
                _initStatus = false;
                return false;
            }
        }

        _initStatus = Interop.Windows.SetInformationJobObject(
            _jobObject,
            _completionPort);

        return _initStatus.Value;
    }

    public void Dispose()
    {
        _jobObject?.Dispose();
        _completionPort?.Dispose();
    }
}
#endif


Review comment (suggestion):
We could call `WaitForExit()` with invalid handle.
```suggestion
            _completionPort = Interop.Windows.CreateIoCompletionPort();
            if (_completionPort.IsInvalid)
            {
                _initStatus = false;
                _completionPort.Dispose();
                _completionPort = null;
                return false;
            }
```

Code after:
    {
        if (_initStatus.HasValue)
        {
            return _initStatus.Value;
        }

        if (_jobObject is null)
        {
            _jobObject = Interop.Windows.CreateJobObject();
            if (_jobObject.IsInvalid)
            {
                _initStatus = false;
                _jobObject.Dispose();
                _jobObject = null;
                return false;
            }
        }

        if (_completionPort is null)
        {
            _completionPort = Interop.Windows.CreateIoCompletionPort();
            if (_completionPort.IsInvalid)
            {
                _initStatus = false;
                _completionPort.Dispose();
                _completionPort = null;
                return false;
            }
        }

        _initStatus = Interop.Windows.SetInformationJobObject(
            _jobObject,
            _completionPort);

        return _initStatus.Value;
    }

    public void Dispose()
    {
        _jobObject?.Dispose();
        _completionPort?.Dispose();
    }
}
#endif
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Interop/Windows/CreateIoCompletionPort.cs (C#)

Code before:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System.Runtime.InteropServices;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        internal sealed class SafeIoCompletionPort : SafeHandle
        {
            public SafeIoCompletionPort() : base(nint.Zero, true) { }

            public override bool IsInvalid => handle == nint.Zero;

            protected override bool ReleaseHandle()
                => Windows.CloseHandle(handle);
        }

        [LibraryImport("kernel32.dll", SetLastError = true)]
        private static partial SafeIoCompletionPort CreateIoCompletionPort(
            nint FileHandle,
            nint ExistingCompletionPort,
            nint CompletionKey,
            int NumberOfConcurrentThreads);

        internal static SafeIoCompletionPort CreateIoCompletionPort()
        {
            return CreateIoCompletionPort(
                FileHandle: -1,
                ExistingCompletionPort: nint.Zero,
                CompletionKey: nint.Zero,
                NumberOfConcurrentThreads: 1);
        }
    }
}


Review comment (suggestion):
```suggestion
            public SafeIoCompletionPort() : base(invalidHandleValue: nint.Zero, ownsHandle: true) { }
```

Code after:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System.Runtime.InteropServices;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        internal sealed class SafeIoCompletionPort : SafeHandle
        {
            public SafeIoCompletionPort() : base(invalidHandleValue: nint.Zero, ownsHandle: true) { }

            public override bool IsInvalid => handle == nint.Zero;

            protected override bool ReleaseHandle()
                => Windows.CloseHandle(handle);
        }

        [LibraryImport("kernel32.dll", SetLastError = true)]
        private static partial SafeIoCompletionPort CreateIoCompletionPort(
            nint FileHandle,
            nint ExistingCompletionPort,
            nint CompletionKey,
            int NumberOfConcurrentThreads);

        internal static SafeIoCompletionPort CreateIoCompletionPort()
        {
            return CreateIoCompletionPort(
                FileHandle: -1,
                ExistingCompletionPort: nint.Zero,
                CompletionKey: nint.Zero,
                NumberOfConcurrentThreads: 1);
        }
    }
}
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Interop/Windows/CreateJobObject.cs (C#)

Code before:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System.Runtime.InteropServices;

internal static partial class Interop
{
    internal static partial class Windows
    {
        internal sealed class SafeJobHandle : SafeHandle
        {
            public SafeJobHandle() : base(nint.Zero, true) { }

            public override bool IsInvalid => handle == nint.Zero;

            protected override bool ReleaseHandle()
                => Windows.CloseHandle(handle);
        }

        [LibraryImport("kernel32.dll", EntryPoint = "CreateJobObjectW", SetLastError = true)]
        private static partial SafeJobHandle CreateJobObject(
            nint lpJobAttributes,
            nint lpName);

        internal static SafeJobHandle CreateJobObject()
            => CreateJobObject(nint.Zero, nint.Zero);
    }
}


Review comment (suggestion):
```suggestion
            public SafeJobHandle() : base(invalidHandleValue: nint.Zero, ownsHandle: true) { }
```

Code after:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System.Runtime.InteropServices;

internal static partial class Interop
{
    internal static partial class Windows
    {
        internal sealed class SafeJobHandle : SafeHandle
        {
            public SafeJobHandle() : base(invalidHandleValue: nint.Zero, ownsHandle: true) { }

            public override bool IsInvalid => handle == nint.Zero;

            protected override bool ReleaseHandle()
                => Windows.CloseHandle(handle);
        }

        [LibraryImport("kernel32.dll", EntryPoint = "CreateJobObjectW", SetLastError = true)]
        private static partial SafeJobHandle CreateJobObject(
            nint lpJobAttributes,
            nint lpName);

        internal static SafeJobHandle CreateJobObject()
            => CreateJobObject(nint.Zero, nint.Zero);
    }
}
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/ScopeArgumentCompleter.cs (C#)

Code before:
{
    /// <summary>
    /// Provides argument completion for Scope parameter.
    /// </summary>
    public class ScopeArgumentCompleter : IArgumentCompleter
    {
        private static readonly string[] s_Scopes = new string[] { "Global", "Local", "Script" };

        /// <summary>
        /// Returns completion results for scope parameter.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="wordToComplete">The word to complete.</param>
        /// <param name="commandAst">The command AST.</param>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of completion results.</returns>
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters)
                => CompletionCompleters.EnumerateQuotedAndUnquotedCompletionText(
                    wordToComplete,
                    possibleCompletionValues: s_Scopes);
    }
}


Review comment (suggestion):
```suggestion
                    possibleCompletionValues: s_Scopes,
                    resultType: CompletionResultType.ParameterValue);
```

Code after:
{
    /// <summary>
    /// Provides argument completion for Scope parameter.
    /// </summary>
    public class ScopeArgumentCompleter : IArgumentCompleter
    {
        private static readonly string[] s_Scopes = new string[] { "Global", "Local", "Script" };

        /// <summary>
        /// Returns completion results for scope parameter.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="wordToComplete">The word to complete.</param>
        /// <param name="commandAst">The command AST.</param>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of completion results.</returns>
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters)
                => CompletionCompleters.GetMatchingResults(
                    wordToComplete,
                    possibleCompletionValues: s_Scopes);
    }
}
Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/InternalCommands.cs (C#)

Code before:

    /// <summary>
    /// Provides argument completion for StrictMode Version parameter.
    /// </summary>
    public class StrictModeVersionArgumentCompleter : IArgumentCompleter
    {
        private static readonly string[] s_strictModeVersions = new string[] { "Latest", "3.0", "2.0", "1.0" };

        /// <summary>
        /// Returns completion results for version parameter.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="wordToComplete">The word to complete.</param>
        /// <param name="commandAst">The command AST.</param>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of Completion Results.</returns>
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters)
                => CompletionCompleters.EnumerateQuotedAndUnquotedCompletionText(
                    wordToComplete,
                    possibleCompletionValues: s_strictModeVersions);
    }

    #endregion Set-StrictMode

    #endregion Built-in cmdlets that are used by or require direct access to the engine.
}


Review comment (suggestion):
```suggestion
                    possibleCompletionValues: s_strictModeVersions,
                    resultType: CompletionResultType.ParameterValue);
```

Code after:

    /// <summary>
    /// Provides argument completion for StrictMode Version parameter.
    /// </summary>
    public class StrictModeVersionArgumentCompleter : IArgumentCompleter
    {
        private static readonly string[] s_strictModeVersions = new string[] { "Latest", "3.0", "2.0", "1.0" };

        /// <summary>
        /// Returns completion results for version parameter.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="wordToComplete">The word to complete.</param>
        /// <param name="commandAst">The command AST.</param>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of Completion Results.</returns>
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters)
                => CompletionCompleters.GetMatchingResults(
                    wordToComplete,
                    possibleCompletionValues: s_strictModeVersions);
    }

    #endregion Set-StrictMode

    #endregion Built-in cmdlets that are used by or require direct access to the engine.
}