diff --git a/.globalconfig b/.globalconfig index 80a1131796f..a658fe94bd9 100644 --- a/.globalconfig +++ b/.globalconfig @@ -588,8 +588,8 @@ dotnet_diagnostic.CA2207.severity = warning # CA2208: Instantiate argument exceptions correctly # https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2208 -dotnet_diagnostic.CA2208.severity = suggestion -dotnet_code_quality.CA2208.api_surface = all +dotnet_diagnostic.CA2208.severity = warning +dotnet_code_quality.ca2208.api_surface = all # CA2211: Non-constant fields should not be visible # https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2211 diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6b33142457..4eda821c5fb 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -578,7 +578,9 @@ private void ProcessMTUSize(string targetNameOrAddress) WriteObject(new PingMtuStatus( Source, resolvedTargetName, +#pragma warning disable CA2208 // Instantiate argument exceptions correctly replyResult ?? throw new ArgumentNullException(nameof(replyResult)), +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 CurrentMTUSize)); } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs index e4d36c66106..9a9d11c150c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs @@ -92,7 +92,9 @@ public ConsoleColorCmdlet() private static ErrorRecord BuildOutOfRangeErrorRecord(object val, string errorId) { string msg = StringUtil.Format(HostStrings.InvalidColorErrorTemplate, val.ToString()); +#pragma warning disable CA2208 // Instantiate argument exceptions correctly ArgumentOutOfRangeException e = new("value", val, msg); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 return new ErrorRecord(e, errorId, ErrorCategory.InvalidArgument, null); } #endregion helper diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs index 28d117f639f..bb728832596 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs @@ -217,7 +217,9 @@ private List ResolvePath(string path, bool isLiteral) { string errorMessage = StringUtil.Format(ConvertMarkdownStrings.FileSystemPathsOnly, path); ErrorRecord errorRecord = new( +#pragma warning disable CA2208 // Instantiate argument exceptions correctly new ArgumentException(), +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 "OnlyFileSystemPathsSupported", ErrorCategory.InvalidArgument, path); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index d6e0b004bdf..9d09b243515 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -544,7 +544,7 @@ public void CopyTo(T[] array, int arrayIndex) if (Count > (array.Length - arrayIndex)) { - throw new ArgumentException("arrayIndex"); + throw new ArgumentException(null, nameof(arrayIndex)); } // Iterate through the buffer in correct order. diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index c3e2e196cbb..f7535c83f18 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -1187,7 +1187,9 @@ private void SetCommandLineError(string msg, bool showHelp = false, bool showBan { if (_error != null) { +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentException(nameof(SetCommandLineError), nameof(_error)); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } _error = msg; diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs index f26a20ae6a9..539e5d15ee2 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs @@ -337,7 +337,9 @@ internal void VerifyDataNotNull(object obj, string name) string msg = StringUtil.Format(FormatAndOut_format_xxx.FOD_NullDataMember, name); ErrorRecord errorRecord = new ErrorRecord( +#pragma warning disable CA2208 // Instantiate argument exceptions correctly new ArgumentException(), +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 "FormatObjectDeserializerNullDataMember", ErrorCategory.InvalidData, null); diff --git a/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs b/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs index 84a31b26426..87c53b6e433 100644 --- a/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs +++ b/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs @@ -223,7 +223,9 @@ public unsafe void CopyFromIndirect(object value) break; default: +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentException(); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } } diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index c73f6e64106..616849577ad 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -2017,7 +2017,9 @@ private static List GetResultForIdentifier(CompletionContext c case UsingStatementKind.Type: break; default: +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentOutOfRangeException("UsingStatementKind"); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } } } diff --git a/src/System.Management.Automation/engine/parser/ast.cs b/src/System.Management.Automation/engine/parser/ast.cs index 54424ea4cc7..0bc9be0a82f 100644 --- a/src/System.Management.Automation/engine/parser/ast.cs +++ b/src/System.Management.Automation/engine/parser/ast.cs @@ -5589,7 +5589,7 @@ public PipelineChainAst( if (chainOperator != TokenKind.AndAnd && chainOperator != TokenKind.OrOr) { - throw new ArgumentException(nameof(chainOperator)); + throw new ArgumentException(null, nameof(chainOperator)); } LhsPipelineChain = lhsChain; diff --git a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs index e270a3efeb2..7fd429d91bb 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs @@ -1661,7 +1661,7 @@ internal SSHClientSessionTransportManager( PSRemotingCryptoHelper cryptoHelper) : base(runspaceId, cryptoHelper) { - if (connectionInfo == null) { throw new PSArgumentException("connectionInfo"); } + if (connectionInfo == null) { throw new PSArgumentException(null, nameof(connectionInfo)); } _connectionInfo = connectionInfo; } diff --git a/src/System.Management.Automation/engine/runtime/MutableTuple.cs b/src/System.Management.Automation/engine/runtime/MutableTuple.cs index 49c5eadc4aa..2aea0d1977e 100644 --- a/src/System.Management.Automation/engine/runtime/MutableTuple.cs +++ b/src/System.Management.Automation/engine/runtime/MutableTuple.cs @@ -365,7 +365,7 @@ internal static IEnumerable GetAccessProperties(Type tupleType, in { // ContractUtils.RequiresNotNull(tupleType, "tupleType"); - if (index < 0 || index >= size) throw new ArgumentException("index"); + if (index < 0 || index >= size) throw new ArgumentException(null, nameof(index)); foreach (int curIndex in GetAccessPath(size, index)) { diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index e528a9bb5a6..f1f9d201509 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -6699,7 +6699,9 @@ public IContentReader GetContentReader(string path) if (usingByteEncoding) { Exception e = +#pragma warning disable CA2208 // Instantiate argument exceptions correctly new ArgumentException(FileSystemProviderStrings.DelimiterError, "delimiter"); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 WriteError(new ErrorRecord( e, "GetContentReaderArgumentError", diff --git a/src/System.Management.Automation/utils/MshTraceSource.cs b/src/System.Management.Automation/utils/MshTraceSource.cs index 8ed5949c31a..f69d77c09be 100644 --- a/src/System.Management.Automation/utils/MshTraceSource.cs +++ b/src/System.Management.Automation/utils/MshTraceSource.cs @@ -187,7 +187,7 @@ internal static PSTraceSource GetNewTraceSource( // Note, all callers should have already verified the name before calling this // API, so this exception should never be exposed to an end-user. - throw new ArgumentException("name"); + throw new ArgumentException(null, nameof(name)); } // Keep the fullName as it was passed, but truncate or pad diff --git a/src/TypeCatalogGen/TypeCatalogGen.cs b/src/TypeCatalogGen/TypeCatalogGen.cs index 8749c19814b..732b02a7cbb 100644 --- a/src/TypeCatalogGen/TypeCatalogGen.cs +++ b/src/TypeCatalogGen/TypeCatalogGen.cs @@ -353,7 +353,9 @@ private static string ResolveTargetFilePath(string path) { if (string.IsNullOrWhiteSpace(path)) { +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentNullException(Param_TargetCSharpFilePath); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } string targetPath = Path.GetFullPath(path); diff --git a/src/powershell/Program.cs b/src/powershell/Program.cs index f5c109a1a76..cf8ba6f2cd0 100644 --- a/src/powershell/Program.cs +++ b/src/powershell/Program.cs @@ -121,7 +121,9 @@ private static void AttemptExecPwshLogin(string[] args) if (pwshPath == null) { +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentNullException(nameof(pwshPath)); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } // exec pwsh @@ -209,7 +211,9 @@ private static void AttemptExecPwshLogin(string[] args) if (pwshPath == null) { +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentNullException(nameof(pwshPath)); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } // exec pwsh