diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs index 24b9332bde5..c08fa0c44ea 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs @@ -149,10 +149,8 @@ public void RegisterCimIndication( uint operationTimeout) { DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression); - if (cimSession == null) - { - throw new ArgumentNullException(string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); - } + + ArgumentNullException.ThrowIfNull(cimSession, string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); this.TargetComputerName = cimSession.ComputerName; CimSessionProxy proxy = CreateSessionProxy(cimSession, operationTimeout); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs index 955d7f8613f..61afc00a676 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs @@ -371,10 +371,7 @@ internal static class ValidationHelper /// public static void ValidateNoNullArgument(object obj, string argumentName) { - if (obj == null) - { - throw new ArgumentNullException(argumentName); - } + ArgumentNullException.ThrowIfNull(obj, argumentName); } /// diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index b0839c6ccd2..4bc9ebef28d 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -214,10 +214,7 @@ public static void AddChild(FrameworkElement parent, FrameworkElement element) { ArgumentNullException.ThrowIfNull(element); - if (parent == null) - { - throw new ArgumentNullException("element"); - } + ArgumentNullException.ThrowIfNull(parent, nameof(element)); ContentControl parentContentControl = parent as ContentControl; @@ -370,10 +367,7 @@ public static T FindVisualAncestorData(this DependencyObject obj) /// The specified value is a null reference. public static T FindVisualAncestor(this DependencyObject @object) where T : class { - if (@object == null) - { - throw new ArgumentNullException("object"); - } + ArgumentNullException.ThrowIfNull(@object, nameof(@object)); DependencyObject parent = VisualTreeHelper.GetParent(@object); diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs index cf85d79ae36..2d590904097 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs @@ -62,7 +62,9 @@ public string DefaultValue /// public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { - if (values == null || values.Length != 1) + ArgumentNullException.ThrowIfNull(values); + + if (values.Length != 1) { throw new ArgumentNullException("values"); } diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs index da51550c084..d2899b994d6 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs @@ -79,7 +79,9 @@ public class AllModulesViewModel : INotifyPropertyChanged /// Commands to show. public AllModulesViewModel(Dictionary importedModules, IEnumerable commands) { - if (commands == null || !commands.GetEnumerator().MoveNext()) + ArgumentNullException.ThrowIfNull(commands); + + if (!commands.GetEnumerator().MoveNext()) { throw new ArgumentNullException("commands"); } diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 58a1fbb4c2d..8da4d7f4c78 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -82,7 +82,8 @@ protected TSession[] Session set { - _session = value ?? throw new ArgumentNullException(nameof(value)); + ArgumentNullException.ThrowIfNull(value); + _session = value; _sessionWasSpecified = true; } } diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6b33142457..e11be68156a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -575,10 +575,12 @@ private void ProcessMTUSize(string targetNameOrAddress) } else { + ArgumentNullException.ThrowIfNull(replyResult); + WriteObject(new PingMtuStatus( Source, resolvedTargetName, - replyResult ?? throw new ArgumentNullException(nameof(replyResult)), + replyResult, CurrentMTUSize)); } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 50a88bb6754..a6a2a6290b0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -189,7 +189,7 @@ internal override void ProcessResponse(HttpResponseMessage response) private static RestReturnType CheckReturnType(HttpResponseMessage response) { - if (response == null) { throw new ArgumentNullException(nameof(response)); } + ArgumentNullException.ThrowIfNull(response); RestReturnType rt = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index c3e2e196cbb..9e555bd428b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -833,10 +833,7 @@ internal void Parse(string[] args) for (int i = 0; i < args.Length; i++) { - if (args[i] is null) - { - throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs); - } + ArgumentNullException.ThrowIfNull(args[i], CommandLineParameterParserStrings.NullElementInArgs); } // Indicates that we've called this method on this instance, and that when it's done, the state variables diff --git a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs index 94eb340b21c..1c82891b654 100644 --- a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs +++ b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs @@ -41,8 +41,7 @@ public string Delimiter [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")] set { - if (value == null) - throw new ArgumentNullException(nameof(Delimiter)); + ArgumentNullException.ThrowIfNull(value, nameof(Delimiter)); if (value.Length == 0) throw new ArgumentException(DotNetEventingStrings.Argument_NeedNonemptyDelimiter); diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index de99812b01d..98c76e31c89 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3413,7 +3413,7 @@ private static string NormalizePath(string path, string host) /// private PSObject GetItemValue(string path) { - if (string.IsNullOrEmpty(path) || (path.Length == 0)) + if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs index 2e4e7fe0f9b..5cd38cec25b 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs @@ -580,7 +580,9 @@ public static class PowerShellAssemblyLoadContextInitializer public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths) { if (string.IsNullOrEmpty(basePaths)) + { throw new ArgumentNullException(nameof(basePaths)); + } PowerShellAssemblyLoadContext.InitializeSingleton(basePaths); } diff --git a/src/System.Management.Automation/engine/ComInterop/Helpers.cs b/src/System.Management.Automation/engine/ComInterop/Helpers.cs index 513c3126476..814e93825a7 100644 --- a/src/System.Management.Automation/engine/ComInterop/Helpers.cs +++ b/src/System.Management.Automation/engine/ComInterop/Helpers.cs @@ -35,10 +35,7 @@ internal static class Requires [System.Diagnostics.Conditional("DEBUG")] internal static void NotNull(object value, string paramName) { - if (value == null) - { - throw new ArgumentNullException(paramName); - } + ArgumentNullException.ThrowIfNull(value, paramName); } [System.Diagnostics.Conditional("DEBUG")] diff --git a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs index 48d74667dae..88a2a2ee427 100644 --- a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs +++ b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs @@ -21,8 +21,7 @@ internal class DefaultCommandRuntime : ICommandRuntime2 /// public DefaultCommandRuntime(List outputList) { - if (outputList == null) - throw new System.ArgumentNullException(nameof(outputList)); + ArgumentNullException.ThrowIfNull(outputList); _output = outputList; } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 113ba8121b8..81675d7b663 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -278,7 +278,9 @@ public string[] ParameterName set { - _parameterNames = value ?? throw new ArgumentNullException(nameof(value)); + ArgumentNullException.ThrowIfNull(value); + + _parameterNames = value; _parameterNameWildcards = SessionStateUtilities.CreateWildcardsFromStrings( _parameterNames, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase); diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 2dbb12fb253..e041b0d7006 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3738,11 +3738,7 @@ internal PSSnapInInfo ImportCorePSSnapIn() internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInException warning) { - if (psSnapInInfo == null) - { - ArgumentNullException e = new ArgumentNullException(nameof(psSnapInInfo)); - throw e; - } + ArgumentNullException.ThrowIfNull(psSnapInInfo); // See if the snapin is already loaded. If has been then there will be an entry in the // Assemblies list for it already... @@ -3918,11 +3914,7 @@ internal static Assembly LoadAssemblyFromFile(string fileName) internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module) { - if (assembly == null) - { - ArgumentNullException e = new ArgumentNullException(nameof(assembly)); - throw e; - } + ArgumentNullException.ThrowIfNull(assembly); string assemblyPath = assembly.Location; PSSnapInHelpers.AnalyzePSSnapInAssembly( diff --git a/src/System.Management.Automation/engine/PSClassInfo.cs b/src/System.Management.Automation/engine/PSClassInfo.cs index fd0ca8d8936..c73e9250071 100644 --- a/src/System.Management.Automation/engine/PSClassInfo.cs +++ b/src/System.Management.Automation/engine/PSClassInfo.cs @@ -62,7 +62,9 @@ public sealed class PSClassMemberInfo internal PSClassMemberInfo(string name, string memberType, string defaultValue) { if (string.IsNullOrEmpty(name)) + { throw new ArgumentNullException(nameof(name)); + } this.Name = name; this.TypeName = memberType; diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 93a3e80b74b..b472393ff53 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1740,10 +1740,7 @@ internal static class Requires { internal static void NotNull(object value, string paramName) { - if (value == null) - { - throw new ArgumentNullException(paramName); - } + ArgumentNullException.ThrowIfNull(value, paramName); } internal static void NotNullOrEmpty(string value, string paramName) @@ -1756,7 +1753,9 @@ internal static void NotNullOrEmpty(string value, string paramName) internal static void NotNullOrEmpty(ICollection value, string paramName) { - if (value == null || value.Count == 0) + ArgumentNullException.ThrowIfNull(value, paramName); + + if (value.Count == 0) { throw new ArgumentNullException(paramName); } diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index 3c7d6576aaa..cc2f8ef9138 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -1138,10 +1138,9 @@ public void Begin(bool expectInput, EngineIntrinsics contextToRedirectTo) /// The command you're calling this from (i.e. instance of PSCmdlet or value of $PSCmdlet variable). public void Begin(InternalCommand command) { - if (command == null || command.MyInvocation == null) - { - throw new ArgumentNullException(nameof(command)); - } + ArgumentNullException.ThrowIfNull(command); + + ArgumentNullException.ThrowIfNull(command.MyInvocation, nameof(command)); Begin(command.MyInvocation.ExpectingInput, command.commandRuntime); } diff --git a/src/System.Management.Automation/engine/parser/SafeValues.cs b/src/System.Management.Automation/engine/parser/SafeValues.cs index 0e41f87a318..f551270c372 100644 --- a/src/System.Management.Automation/engine/parser/SafeValues.cs +++ b/src/System.Management.Automation/engine/parser/SafeValues.cs @@ -530,10 +530,10 @@ public object VisitIndexExpression(IndexExpressionAst indexExpressionAst) // Get the value of the index and value and call the compiler var index = indexExpressionAst.Index.Accept(this); var target = indexExpressionAst.Target.Accept(this); - if (index == null || target == null) - { - throw new ArgumentNullException(nameof(indexExpressionAst)); - } + + ArgumentNullException.ThrowIfNull(index, nameof(indexExpressionAst)); + + ArgumentNullException.ThrowIfNull(target, nameof(indexExpressionAst)); return GetIndexedValueFromTarget(target, index); } diff --git a/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs b/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs index 7bb52abcb1e..29d107a074f 100644 --- a/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs +++ b/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs @@ -19,10 +19,7 @@ public abstract class Repository where T : class /// Object to add. public void Add(T item) { - if (item == null) - { - throw new ArgumentNullException(_identifier); - } + ArgumentNullException.ThrowIfNull(item, _identifier); lock (_syncObject) { @@ -45,10 +42,7 @@ public void Add(T item) /// Object to remove. public void Remove(T item) { - if (item == null) - { - throw new ArgumentNullException(_identifier); - } + ArgumentNullException.ThrowIfNull(item, _identifier); lock (_syncObject) { diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 7edf50b5141..0ffc79ff6c4 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7884,7 +7884,6 @@ internal static bool CreateJunction(string path, string target) { throw new ArgumentNullException(nameof(target)); } - using (SafeHandle handle = WinOpenReparsePoint(path, FileAccess.Write)) { byte[] mountPointBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(target)); diff --git a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs index 10826d25b21..32fce575a1b 100644 --- a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs +++ b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs @@ -1388,8 +1388,7 @@ public void SetValue(string name, object value) [ComVisible(false)] public unsafe void SetValue(string name, object value, RegistryValueKind valueKind) { - if (value == null) - throw new ArgumentNullException(RegistryProviderStrings.Arg_Value); + ArgumentNullException.ThrowIfNull(value, RegistryProviderStrings.Arg_Value); if (name != null && name.Length > MaxValueNameLength) { @@ -2031,10 +2030,7 @@ private RegistryKeyPermissionCheck GetSubKeyPermissionCheck(bool subkeyWritable) private static void ValidateKeyName(string name) { - if (name == null) - { - throw new ArgumentNullException(RegistryProviderStrings.Arg_Name); - } + ArgumentNullException.ThrowIfNull(name, RegistryProviderStrings.Arg_Name); int nextSlash = name.IndexOf('\\'); int current = 0; diff --git a/src/System.Management.Automation/utils/BackgroundDispatcher.cs b/src/System.Management.Automation/utils/BackgroundDispatcher.cs index 8ef105ce0ea..ac74a32d352 100644 --- a/src/System.Management.Automation/utils/BackgroundDispatcher.cs +++ b/src/System.Management.Automation/utils/BackgroundDispatcher.cs @@ -70,7 +70,8 @@ public BackgroundDispatcher(EventProvider transferProvider, EventDescriptor tran // internal for unit testing only. Otherwise, would be private. internal BackgroundDispatcher(IMethodInvoker etwActivityMethodInvoker) { - _etwActivityMethodInvoker = etwActivityMethodInvoker ?? throw new ArgumentNullException(nameof(etwActivityMethodInvoker)); + ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker); + _etwActivityMethodInvoker = etwActivityMethodInvoker; _invokerWaitCallback = DoInvoker; } diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index 4c953be0276..b7056108218 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -131,7 +131,9 @@ public ParseException(string message, Justification = "ErrorRecord is not overridden in classes deriving from ParseException")] public ParseException(Language.ParseError[] errors) { - if ((errors == null) || (errors.Length == 0)) + ArgumentNullException.ThrowIfNull(errors); + + if (errors.Length == 0) { throw new ArgumentNullException(nameof(errors)); } diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index 917e04a5e69..33e03e67a47 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -107,8 +107,10 @@ protected CounterSetRegistrarBase( CounterSetId = counterSetId; CounterSetInstType = counterSetInstType; CounterSetName = counterSetName; - if ((counterInfoArray == null) - || (counterInfoArray.Length == 0)) + + ArgumentNullException.ThrowIfNull(counterInfoArray); + + if (counterInfoArray.Length == 0) { throw new ArgumentNullException(nameof(counterInfoArray)); }