From b42734ddabf0d8c7808766a52ac2024dbebcf9e9 Mon Sep 17 00:00:00 2001 From: Dmitry Volodin Date: Sun, 21 May 2023 19:28:36 +0400 Subject: [PATCH] Apply IDE0019: InlineAsTypeCheck in Microsoft.PowerShell folder --- .../host/msh/ConsoleHost.cs | 27 ++-- .../host/msh/ConsoleHostRawUserInterface.cs | 3 +- .../host/msh/Executor.cs | 9 +- .../DotNetCode/Eventing/EventProvider.cs | 3 +- .../security/AclCommands.cs | 6 +- .../security/CertificateProvider.cs | 129 +++++++----------- 6 files changed, 67 insertions(+), 110 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 409869cd79e..04fcffaa5be 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -2141,9 +2141,7 @@ private void ReportException(Exception e, Executor exec) // NTRAID#Windows OS Bugs-1143621-2005/04/08-sburns - IContainsErrorRecord icer = e as IContainsErrorRecord; - - if (icer != null) + if (e is IContainsErrorRecord icer) { error = icer.ErrorRecord; } @@ -2200,8 +2198,7 @@ private void ReportExceptionFallback(Exception e, string header) // See if the exception has an error record attached to it... ErrorRecord er = null; - IContainsErrorRecord icer = e as IContainsErrorRecord; - if (icer != null) + if (e is IContainsErrorRecord icer) er = icer.ErrorRecord; if (e is PSRemotingTransportException) @@ -2736,8 +2733,7 @@ e is RemoteException || internal void BlockCommandOutput() { - RemotePipeline rCmdPipeline = _parent.runningCmd as RemotePipeline; - if (rCmdPipeline != null) + if (_parent.runningCmd is RemotePipeline rCmdPipeline) { rCmdPipeline.DrainIncomingData(); rCmdPipeline.SuspendIncomingData(); @@ -2750,8 +2746,7 @@ internal void BlockCommandOutput() internal void ResumeCommandOutput() { - RemotePipeline rCmdPipeline = _parent.runningCmd as RemotePipeline; - if (rCmdPipeline != null) + if (_parent.runningCmd is RemotePipeline rCmdPipeline) { rCmdPipeline.ResumeIncomingData(); } @@ -2841,8 +2836,7 @@ private static bool IsIncompleteParseException(Exception e) } // If it is remote exception ferret out the real exception. - RemoteException remoteException = e as RemoteException; - if (remoteException == null || remoteException.ErrorRecord == null) + if (e is not RemoteException remoteException || remoteException.ErrorRecord == null) { return false; } @@ -2923,8 +2917,7 @@ private string EvaluatePrompt() // Check for the pushed runspace scenario. if (_isRunspacePushed) { - RemoteRunspace remoteRunspace = _parent.Runspace as RemoteRunspace; - if (remoteRunspace != null) + if (_parent.Runspace is RemoteRunspace remoteRunspace) { promptString = HostUtilities.GetRemotePrompt(remoteRunspace, promptString, _parent._inPushedConfiguredSession); } @@ -2958,13 +2951,9 @@ private string EvaluateDebugPrompt() PSObject prompt = output.ReadAndRemoveAt0(); string promptString = (prompt != null) ? (prompt.BaseObject as string) : null; - if (promptString != null) + if (promptString != null && _parent.Runspace is RemoteRunspace remoteRunspace) { - RemoteRunspace remoteRunspace = _parent.Runspace as RemoteRunspace; - if (remoteRunspace != null) - { - promptString = HostUtilities.GetRemotePrompt(remoteRunspace, promptString, _parent._inPushedConfiguredSession); - } + promptString = HostUtilities.GetRemotePrompt(remoteRunspace, promptString, _parent._inPushedConfiguredSession); } return promptString; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs index a0d654759b7..58630f5b3c0 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs @@ -354,8 +354,7 @@ public override } catch (HostException e) { - Win32Exception win32exception = e.InnerException as Win32Exception; - if (win32exception != null && + if (e.InnerException is Win32Exception win32exception && win32exception.NativeErrorCode == 0x57) { throw PSTraceSource.NewArgumentOutOfRangeException("value", value, diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs index 013281d2209..43cd165cc87 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs @@ -107,8 +107,7 @@ private void ErrorObjectStreamHandler(object sender, EventArgs e) private void AsyncPipelineFailureHandler(Exception ex) { ErrorRecord er = null; - IContainsErrorRecord cer = ex as IContainsErrorRecord; - if (cer != null) + if (ex is IContainsErrorRecord cer) { er = cer.ErrorRecord; // Exception inside the error record is ParentContainsErrorRecordException which @@ -483,8 +482,7 @@ internal string ExecuteCommandAndGetResultAsString(string command, out Exception // And convert the base object into a string. We can't use the proxied // ToString() on the PSObject because there is no default runspace // available. - PSObject msho = streamResults[0] as PSObject; - if (msho != null) + if (streamResults[0] is PSObject msho) result = msho.BaseObject.ToString(); else result = streamResults[0].ToString(); @@ -585,8 +583,7 @@ private void Cancel() internal void BlockCommandOutput() { - RemotePipeline remotePipeline = _pipeline as RemotePipeline; - if (remotePipeline != null) + if (_pipeline is RemotePipeline remotePipeline) { // Waits until queued data is handled. remotePipeline.DrainIncomingData(); diff --git a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProvider.cs b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProvider.cs index af32014ab9d..32ce9993cfb 100644 --- a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProvider.cs +++ b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProvider.cs @@ -294,8 +294,7 @@ to fill the passed in ETW data descriptor. { dataDescriptor->Reserved = 0; - string sRet = data as string; - if (sRet != null) + if (data is string sRet) { dataDescriptor->Size = (uint)((sRet.Length + 1) * 2); return sRet; diff --git a/src/Microsoft.PowerShell.Security/security/AclCommands.cs b/src/Microsoft.PowerShell.Security/security/AclCommands.cs index c0994a57a53..8cff78e1bfc 100644 --- a/src/Microsoft.PowerShell.Security/security/AclCommands.cs +++ b/src/Microsoft.PowerShell.Security/security/AclCommands.cs @@ -291,8 +291,7 @@ public static AuthorizationRuleCollection GetAccess(PSObject instance) } // Get DACL - CommonObjectSecurity cos = sd as CommonObjectSecurity; - if (cos != null) + if (sd is CommonObjectSecurity cos) { return cos.GetAccessRules(true, true, typeof(NTAccount)); } @@ -326,8 +325,7 @@ public static AuthorizationRuleCollection GetAudit(PSObject instance) PSTraceSource.NewArgumentException(nameof(instance)); } - CommonObjectSecurity cos = sd as CommonObjectSecurity; - if (cos != null) + if (sd is CommonObjectSecurity cos) { return cos.GetAuditRules(true, true, typeof(NTAccount)); } diff --git a/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs b/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs index 7644f3de620..527bf6be675 100644 --- a/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs +++ b/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs @@ -724,16 +724,11 @@ protected override void RemoveItem( ThrowInvalidOperation(errorId, message); } - if (DynamicParameters != null) + if (DynamicParameters != null && DynamicParameters is ProviderRemoveItemDynamicParameters dp) { - ProviderRemoveItemDynamicParameters dp = - DynamicParameters as ProviderRemoveItemDynamicParameters; - if (dp != null) + if (dp.DeleteKey) { - if (dp.DeleteKey) - { - fDeleteKey = true; - } + fDeleteKey = true; } } @@ -888,9 +883,8 @@ protected override void MoveItem( object store = GetItemAtPath(destination, false, out isDestContainer); X509Certificate2 certificate = cert as X509Certificate2; - X509NativeStore certstore = store as X509NativeStore; - if (certstore != null) + if (store is X509NativeStore certstore) { certstore.Open(true); @@ -1068,23 +1062,18 @@ protected override bool HasChildItems(string path) if ((item != null) && isContainer) { - X509StoreLocation storeLocation = item as X509StoreLocation; - if (storeLocation != null) + if (item is X509StoreLocation storeLocation) { result = storeLocation.StoreNames.Count > 0; } - else + else if (item is X509NativeStore store) { - X509NativeStore store = item as X509NativeStore; - if (store != null) + store.Open(IncludeArchivedCerts()); + IntPtr certContext = store.GetFirstCert(); + if (certContext != IntPtr.Zero) { - store.Open(IncludeArchivedCerts()); - IntPtr certContext = store.GetFirstCert(); - if (certContext != IntPtr.Zero) - { - store.FreeCert(certContext); - result = true; - } + store.FreeCert(certContext); + result = true; } } } @@ -1259,20 +1248,15 @@ protected override void GetItem(string path) return; } - X509StoreLocation storeLocation = item as X509StoreLocation; - if (storeLocation != null) // store location + if (item is X509StoreLocation storeLocation) // store location { WriteItemObject(item, path, isContainer); } - else // store + else if (item is X509NativeStore store) // store { - X509NativeStore store = item as X509NativeStore; - if (store != null) - { - // create X509Store - X509Store outStore = new(store.StoreName, store.Location.Location); - WriteItemObject(outStore, path, isContainer); - } + // create X509Store + X509Store outStore = new(store.StoreName, store.Location.Location); + WriteItemObject(outStore, path, isContainer); } } } @@ -2644,51 +2628,46 @@ private CertificateFilterInfo GetFilter() { CertificateFilterInfo filter = null; - if (DynamicParameters != null) + if (DynamicParameters != null && DynamicParameters is CertificateProviderDynamicParameters dp) { - CertificateProviderDynamicParameters dp = - DynamicParameters as CertificateProviderDynamicParameters; - if (dp != null) + if (dp.CodeSigningCert) { - if (dp.CodeSigningCert) - { - filter = new CertificateFilterInfo(); - filter.Purpose = CertificatePurpose.CodeSigning; - } + filter = new CertificateFilterInfo(); + filter.Purpose = CertificatePurpose.CodeSigning; + } - if (dp.DocumentEncryptionCert) - { - filter ??= new CertificateFilterInfo(); - filter.Purpose = CertificatePurpose.DocumentEncryption; - } + if (dp.DocumentEncryptionCert) + { + filter ??= new CertificateFilterInfo(); + filter.Purpose = CertificatePurpose.DocumentEncryption; + } - if (dp.DnsName != null) - { - filter ??= new CertificateFilterInfo(); - filter.DnsName = new WildcardPattern(dp.DnsName, WildcardOptions.IgnoreCase); - } + if (dp.DnsName != null) + { + filter ??= new CertificateFilterInfo(); + filter.DnsName = new WildcardPattern(dp.DnsName, WildcardOptions.IgnoreCase); + } - if (dp.Eku != null) + if (dp.Eku != null) + { + filter ??= new CertificateFilterInfo(); + filter.Eku = new List(); + foreach (var pattern in dp.Eku) { - filter ??= new CertificateFilterInfo(); - filter.Eku = new List(); - foreach (var pattern in dp.Eku) - { - filter.Eku.Add(new WildcardPattern(pattern, WildcardOptions.IgnoreCase)); - } + filter.Eku.Add(new WildcardPattern(pattern, WildcardOptions.IgnoreCase)); } + } - if (dp.ExpiringInDays >= 0) - { - filter ??= new CertificateFilterInfo(); - filter.Expiring = DateTime.Now.AddDays(dp.ExpiringInDays); - } + if (dp.ExpiringInDays >= 0) + { + filter ??= new CertificateFilterInfo(); + filter.Expiring = DateTime.Now.AddDays(dp.ExpiringInDays); + } - if (dp.SSLServerAuthentication) - { - filter ??= new CertificateFilterInfo(); - filter.SSLServerAuthentication = true; - } + if (dp.SSLServerAuthentication) + { + filter ??= new CertificateFilterInfo(); + filter.SSLServerAuthentication = true; } } @@ -3311,17 +3290,13 @@ public EnhancedKeyUsageProperty(X509Certificate2 cert) foreach (X509Extension extension in cert.Extensions) { // Filter to the OID for EKU - if (extension.Oid.Value == "2.5.29.37") + if (extension.Oid.Value == "2.5.29.37" && extension is X509EnhancedKeyUsageExtension ext) { - X509EnhancedKeyUsageExtension ext = extension as X509EnhancedKeyUsageExtension; - if (ext != null) + OidCollection oids = ext.EnhancedKeyUsages; + foreach (Oid oid in oids) { - OidCollection oids = ext.EnhancedKeyUsages; - foreach (Oid oid in oids) - { - EnhancedKeyUsageRepresentation ekuString = new(oid.FriendlyName, oid.Value); - _ekuList.Add(ekuString); - } + EnhancedKeyUsageRepresentation ekuString = new(oid.FriendlyName, oid.Value); + _ekuList.Add(ekuString); } } }