From 2c09740e42230fa1e5d5bdf65deaa8ee9666bdec Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Mon, 26 Oct 2020 21:24:02 +0000 Subject: [PATCH 1/3] Enable: CA2208: Instantiate argument exceptions correctly https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2208 --- .globalconfig | 3 ++- .../commands/utility/MatchString.cs | 2 +- src/System.Management.Automation/engine/parser/ast.cs | 2 +- .../engine/remoting/fanin/OutOfProcTransportManager.cs | 2 +- .../engine/runtime/MutableTuple.cs | 2 +- src/System.Management.Automation/utils/MshTraceSource.cs | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.globalconfig b/.globalconfig index 8005c26b971..976d0af0060 100644 --- a/.globalconfig +++ b/.globalconfig @@ -556,7 +556,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_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.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 6271e6f57b6..9dc7067db42 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/System.Management.Automation/engine/parser/ast.cs b/src/System.Management.Automation/engine/parser/ast.cs index 3e1f9ce60a5..2a6a348342c 100644 --- a/src/System.Management.Automation/engine/parser/ast.cs +++ b/src/System.Management.Automation/engine/parser/ast.cs @@ -5422,7 +5422,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 2d92e520167..6b378363a21 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs @@ -1606,7 +1606,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/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 From 213fe80f73c50d4557e7c857c63759df0c7d610e Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Tue, 27 Oct 2020 20:26:24 +0000 Subject: [PATCH 2/3] Suppress rule violations --- .../commands/management/TestConnectionCommand.cs | 4 ++++ .../commands/utility/ConsoleColorCmdlet.cs | 4 ++++ .../commands/utility/ConvertFromMarkdownCommand.cs | 4 ++++ .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++++ .../host/msh/CommandLineParameterParser.cs | 4 ++++ .../FormatAndOutput/common/FormattingObjectsDeserializer.cs | 4 ++++ .../engine/ComInterop/InteropServices/Variant.cs | 4 ++++ .../engine/CommandCompletion/CompletionAnalysis.cs | 4 ++++ .../namespaces/FileSystemProvider.cs | 4 ++++ src/TypeCatalogGen/TypeCatalogGen.cs | 4 ++++ src/powershell/Program.cs | 4 ++++ 11 files changed, 44 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6b33142457..b4b193fac30 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -478,6 +478,10 @@ private void ProcessTraceroute(string targetNameOrAddress) #endregion TracerouteTest #region MTUSizeTest + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private void ProcessMTUSize(string targetNameOrAddress) { PingReply? reply, replyResult = null; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs index e4d36c66106..06d7f88ff9e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs @@ -89,6 +89,10 @@ public ConsoleColorCmdlet() } #region helper + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private static ErrorRecord BuildOutOfRangeErrorRecord(object val, string errorId) { string msg = StringUtil.Format(HostStrings.InvalidColorErrorTemplate, val.ToString()); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs index 28d117f639f..1ec08ba2f1d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs @@ -185,6 +185,10 @@ private async Task ReadContentFromFile(string filePath) return null; } + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private List ResolvePath(string path, bool isLiteral) { ProviderInfo provider = null; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 3423b912fe7..ca2ba3945ca 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1861,6 +1861,10 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr /// The Field Value to use. /// The > to update. /// If true, collection types in will be enumerated. If false, collections will be treated as single value. + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private void AddMultipartContent(object fieldName, object fieldValue, MultipartFormDataContent formData, bool enumerate) { if (formData == null) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index a63c9193345..d224ab0c673 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -1007,6 +1007,10 @@ internal void ShowErrorHelpBanner(PSHostUserInterface hostUI, string? bannerText DisplayBanner(hostUI, bannerText); } + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private void SetCommandLineError(string msg, bool showHelp = false, bool showBanner = false) { if (_error != null) diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs index 962cf6f5ff1..8b1f733d8e4 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs @@ -331,6 +331,10 @@ internal FormatInfoData DeserializeObject(PSObject so) return fid; } + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] internal void VerifyDataNotNull(object obj, string name) { if (obj != 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..919b30cf6fc 100644 --- a/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs +++ b/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs @@ -119,6 +119,10 @@ public static bool IsPrimitiveType(VarEnum varEnum) return false; } + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] public unsafe void CopyFromIndirect(object value) { VarEnum vt = (VarEnum)(((int)this.VariantType) & ~((int)VarEnum.VT_BYREF)); diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index 76be6418805..92147bdbf03 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -1750,6 +1750,10 @@ private static List GetResultForIdentifierInConfiguration( return results; } + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private List GetResultForIdentifier(CompletionContext completionContext, ref int replacementIndex, ref int replacementLength, bool isQuotedString) { var tokenAtCursor = completionContext.TokenAtCursor; diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index c81991b95e4..3c029ab54b8 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -6557,6 +6557,10 @@ public object ClearPropertyDynamicParameters( /// /// path is null or empty. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] public IContentReader GetContentReader(string path) { if (string.IsNullOrEmpty(path)) diff --git a/src/TypeCatalogGen/TypeCatalogGen.cs b/src/TypeCatalogGen/TypeCatalogGen.cs index 993413bc59a..9bca459a29f 100644 --- a/src/TypeCatalogGen/TypeCatalogGen.cs +++ b/src/TypeCatalogGen/TypeCatalogGen.cs @@ -349,6 +349,10 @@ private static string GetTypeFullName(MetadataReader metadataReader, TypeDefinit /// /// Resolve the target file path. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private static string ResolveTargetFilePath(string path) { if (string.IsNullOrWhiteSpace(path)) diff --git a/src/powershell/Program.cs b/src/powershell/Program.cs index d9dfa2f80bd..f99de213d51 100644 --- a/src/powershell/Program.cs +++ b/src/powershell/Program.cs @@ -80,6 +80,10 @@ public static int Main(string[] args) /// In the event of success, we use an exec() call, so this method never returns. /// /// The startup arguments to pwsh. + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "CA2208:Instantiate argument exceptions correctly", + Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private static void AttemptExecPwshLogin(string[] args) { // If the login environment variable is set, we have already done the login logic and have been exec'd From 6cd8cc582eeb4bffccad726e3bfc905bb976f5a5 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Sat, 3 Jul 2021 11:51:34 +0100 Subject: [PATCH 3/3] Use local suppression Address @iSazonov review --- .../commands/management/TestConnectionCommand.cs | 6 ++---- .../commands/utility/ConsoleColorCmdlet.cs | 6 ++---- .../commands/utility/ConvertFromMarkdownCommand.cs | 6 ++---- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 6 ++---- .../host/msh/CommandLineParameterParser.cs | 6 ++---- .../common/FormattingObjectsDeserializer.cs | 6 ++---- .../engine/ComInterop/InteropServices/Variant.cs | 6 ++---- .../engine/CommandCompletion/CompletionAnalysis.cs | 6 ++---- .../namespaces/FileSystemProvider.cs | 6 ++---- src/TypeCatalogGen/TypeCatalogGen.cs | 6 ++---- src/powershell/Program.cs | 8 ++++---- 11 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index b4b193fac30..4eda821c5fb 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -478,10 +478,6 @@ private void ProcessTraceroute(string targetNameOrAddress) #endregion TracerouteTest #region MTUSizeTest - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private void ProcessMTUSize(string targetNameOrAddress) { PingReply? reply, replyResult = null; @@ -582,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 06d7f88ff9e..9a9d11c150c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConsoleColorCmdlet.cs @@ -89,14 +89,12 @@ public ConsoleColorCmdlet() } #region helper - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] 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 1ec08ba2f1d..bb728832596 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFromMarkdownCommand.cs @@ -185,10 +185,6 @@ private async Task ReadContentFromFile(string filePath) return null; } - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private List ResolvePath(string path, bool isLiteral) { ProviderInfo provider = null; @@ -221,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/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index ca2ba3945ca..f77dd5613d7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1861,15 +1861,13 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr /// The Field Value to use. /// The > to update. /// If true, collection types in will be enumerated. If false, collections will be treated as single value. - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private void AddMultipartContent(object fieldName, object fieldValue, MultipartFormDataContent formData, bool enumerate) { if (formData == null) { +#pragma warning disable CA2208 // Instantiate argument exceptions correctly throw new ArgumentNullException("formDate"); +#pragma warning restore CA2208 // https://github.com/PowerShell/PowerShell/issues/13909 } // It is possible that the dictionary keys or values are PSObject wrapped depending on how the dictionary is defined and assigned. diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index d224ab0c673..ad261ad12a9 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -1007,15 +1007,13 @@ internal void ShowErrorHelpBanner(PSHostUserInterface hostUI, string? bannerText DisplayBanner(hostUI, bannerText); } - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private void SetCommandLineError(string msg, bool showHelp = false, bool showBanner = false) { 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 8b1f733d8e4..10b6334302b 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs @@ -331,10 +331,6 @@ internal FormatInfoData DeserializeObject(PSObject so) return fid; } - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] internal void VerifyDataNotNull(object obj, string name) { if (obj != null) @@ -343,7 +339,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 919b30cf6fc..87c53b6e433 100644 --- a/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs +++ b/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs @@ -119,10 +119,6 @@ public static bool IsPrimitiveType(VarEnum varEnum) return false; } - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] public unsafe void CopyFromIndirect(object value) { VarEnum vt = (VarEnum)(((int)this.VariantType) & ~((int)VarEnum.VT_BYREF)); @@ -227,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 92147bdbf03..d6047eefd07 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -1750,10 +1750,6 @@ private static List GetResultForIdentifierInConfiguration( return results; } - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private List GetResultForIdentifier(CompletionContext completionContext, ref int replacementIndex, ref int replacementLength, bool isQuotedString) { var tokenAtCursor = completionContext.TokenAtCursor; @@ -1812,7 +1808,9 @@ private List GetResultForIdentifier(CompletionContext completi 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/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 3c029ab54b8..b884c138b47 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -6557,10 +6557,6 @@ public object ClearPropertyDynamicParameters( /// /// path is null or empty. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] public IContentReader GetContentReader(string path) { if (string.IsNullOrEmpty(path)) @@ -6661,7 +6657,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/TypeCatalogGen/TypeCatalogGen.cs b/src/TypeCatalogGen/TypeCatalogGen.cs index 9bca459a29f..92a2782c449 100644 --- a/src/TypeCatalogGen/TypeCatalogGen.cs +++ b/src/TypeCatalogGen/TypeCatalogGen.cs @@ -349,15 +349,13 @@ private static string GetTypeFullName(MetadataReader metadataReader, TypeDefinit /// /// Resolve the target file path. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] 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 f99de213d51..b4fe2abf941 100644 --- a/src/powershell/Program.cs +++ b/src/powershell/Program.cs @@ -80,10 +80,6 @@ public static int Main(string[] args) /// In the event of success, we use an exec() call, so this method never returns. /// /// The startup arguments to pwsh. - [System.Diagnostics.CodeAnalysis.SuppressMessage( - "Usage", - "CA2208:Instantiate argument exceptions correctly", - Justification = "https://github.com/PowerShell/PowerShell/issues/13909")] private static void AttemptExecPwshLogin(string[] args) { // If the login environment variable is set, we have already done the login logic and have been exec'd @@ -124,7 +120,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 @@ -212,7 +210,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