Apply IDE0019: InlineAsTypeCheck in Microsoft.PowerShell folder (#19689)

This commit is contained in:
Dmitry Volodin
2023-05-24 17:23:23 +05:00
committed by GitHub
parent fbc64e4818
commit 368a1ea372
6 changed files with 67 additions and 110 deletions
@@ -2149,9 +2149,7 @@ namespace Microsoft.PowerShell
// 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;
}
@@ -2208,8 +2206,7 @@ namespace Microsoft.PowerShell
// 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)
@@ -2744,8 +2741,7 @@ namespace Microsoft.PowerShell
internal void BlockCommandOutput()
{
RemotePipeline rCmdPipeline = _parent.runningCmd as RemotePipeline;
if (rCmdPipeline != null)
if (_parent.runningCmd is RemotePipeline rCmdPipeline)
{
rCmdPipeline.DrainIncomingData();
rCmdPipeline.SuspendIncomingData();
@@ -2758,8 +2754,7 @@ namespace Microsoft.PowerShell
internal void ResumeCommandOutput()
{
RemotePipeline rCmdPipeline = _parent.runningCmd as RemotePipeline;
if (rCmdPipeline != null)
if (_parent.runningCmd is RemotePipeline rCmdPipeline)
{
rCmdPipeline.ResumeIncomingData();
}
@@ -2849,8 +2844,7 @@ namespace Microsoft.PowerShell
}
// 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;
}
@@ -2931,8 +2925,7 @@ namespace Microsoft.PowerShell
// 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);
}
@@ -2966,13 +2959,9 @@ namespace Microsoft.PowerShell
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;
@@ -354,8 +354,7 @@ namespace Microsoft.PowerShell
}
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,
@@ -107,8 +107,7 @@ namespace Microsoft.PowerShell
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 @@ namespace Microsoft.PowerShell
// 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 @@ namespace Microsoft.PowerShell
internal void BlockCommandOutput()
{
RemotePipeline remotePipeline = _pipeline as RemotePipeline;
if (remotePipeline != null)
if (_pipeline is RemotePipeline remotePipeline)
{
// Waits until queued data is handled.
remotePipeline.DrainIncomingData();
@@ -294,8 +294,7 @@ namespace System.Diagnostics.Eventing
{
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;
@@ -291,8 +291,7 @@ namespace Microsoft.PowerShell.Commands
}
// 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 @@ namespace Microsoft.PowerShell.Commands
PSTraceSource.NewArgumentException(nameof(instance));
}
CommonObjectSecurity cos = sd as CommonObjectSecurity;
if (cos != null)
if (sd is CommonObjectSecurity cos)
{
return cos.GetAuditRules(true, true, typeof(NTAccount));
}
@@ -724,16 +724,11 @@ namespace Microsoft.PowerShell.Commands
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 @@ namespace Microsoft.PowerShell.Commands
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 @@ namespace Microsoft.PowerShell.Commands
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 @@ namespace Microsoft.PowerShell.Commands
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 @@ namespace Microsoft.PowerShell.Commands
{
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<WildcardPattern>();
foreach (var pattern in dp.Eku)
{
filter ??= new CertificateFilterInfo();
filter.Eku = new List<WildcardPattern>();
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 @@ namespace Microsoft.PowerShell.Commands
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);
}
}
}