Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ protected object GetReferenceOrReferenceArrayObject(object value, ref CimType re
if (cimReference != null)
{
object baseObject = GetBaseObject(cimReference.Value);
CimInstance cimInstance = baseObject as CimInstance;
if (cimInstance == null)
if (!(baseObject is CimInstance cimInstance))
{
return null;
}
Expand All @@ -412,8 +411,7 @@ protected object GetReferenceOrReferenceArrayObject(object value, ref CimType re
CimInstance[] cimInstanceArray = new CimInstance[cimReferenceArray.Length];
for (int i = 0; i < cimReferenceArray.Length; i++)
{
PSReference tempCimReference = cimReferenceArray[i] as PSReference;
if (tempCimReference == null)
if (!(cimReferenceArray[i] is PSReference tempCimReference))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2085,8 +2085,7 @@ protected override bool PreNewActionEvent(CmdletActionEventArgs args)
}

CimWriteResultObject writeResultObject = args.Action as CimWriteResultObject;
CimClass cimClass = writeResultObject.Result as CimClass;
if (cimClass == null)
if (!(writeResultObject.Result is CimClass cimClass))
{
return true;
}
Expand Down Expand Up @@ -2239,8 +2238,7 @@ protected override bool PreNewActionEvent(CmdletActionEventArgs args)
}

CimWriteResultObject writeResultObject = args.Action as CimWriteResultObject;
CimInstance cimInstance = writeResultObject.Result as CimInstance;
if (cimInstance == null)
if (!(writeResultObject.Result is CimInstance cimInstance))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ private enum WsManErrorCode : uint

private static bool IsWsManQuotaReached(Exception exception)
{
var cimException = exception as CimException;
if (cimException == null)
if (!(exception is CimException cimException))
{
return false;
}
Expand Down Expand Up @@ -1010,8 +1009,7 @@ private CimResponseType PromptUserCallback(string message, CimPromptType promptT
internal static bool IsShowComputerNameMarkerPresent(CimInstance cimInstance)
{
PSObject pso = PSObject.AsPSObject(cimInstance);
PSPropertyInfo psShowComputerNameProperty = pso.InstanceMembers[RemotingConstants.ShowComputerNameNoteProperty] as PSPropertyInfo;
if (psShowComputerNameProperty == null)
if (!(pso.InstanceMembers[RemotingConstants.ShowComputerNameNoteProperty] is PSPropertyInfo psShowComputerNameProperty))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ private CimJobContext CreateJobContext(CimSession session, object targetObject)
/// <returns><see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model.</returns>
internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
{
CimQuery query = baseQuery as CimQuery;
if (query == null)
if (!(baseQuery is CimQuery query))
{
throw new ArgumentNullException(nameof(baseQuery));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,7 @@ private static bool ActualValueGreaterThanOrEqualToExpectedValue(string property
{
try
{
var expectedComparable = expectedPropertyValue as IComparable;
if (expectedComparable == null)
if (!(expectedPropertyValue is IComparable expectedComparable))
{
return false;
}
Expand Down Expand Up @@ -608,8 +607,7 @@ private static bool ActualValueLessThanOrEqualToExpectedValue(string propertyNam
{
try
{
var actualComparable = actualPropertyValue as IComparable;
if (actualComparable == null)
if (!(actualPropertyValue is IComparable actualComparable))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,7 @@ private void WriteMemberInfoCollection(
continue;
}

PSPropertyInfo property = info as PSPropertyInfo;
if (property == null)
if (!(info is PSPropertyInfo property))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ internal Type GetValueType(PSObject liveObject, out object columnValue)
/// <returns>The source string limited in the number of lines.</returns>
internal static object LimitString(object src)
{
string srcString = src as string;
if (srcString == null)
if (!(src is string srcString))
{
return src;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ protected override void ProcessRecord()
Command,
delegate (Breakpoint breakpoint, string command)
{
CommandBreakpoint commandBreakpoint = breakpoint as CommandBreakpoint;

if (commandBreakpoint == null)
if (!(breakpoint is CommandBreakpoint commandBreakpoint))
{
return false;
}
Expand All @@ -137,9 +135,7 @@ protected override void ProcessRecord()
Variable,
delegate (Breakpoint breakpoint, string variable)
{
VariableBreakpoint variableBreakpoint = breakpoint as VariableBreakpoint;

if (variableBreakpoint == null)
if (!(breakpoint is VariableBreakpoint variableBreakpoint))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2623,8 +2623,7 @@ private string GenerateConnectionStringForNewRunspace()

private string GenerateAllowRedirectionParameter()
{
WSManConnectionInfo wsmanConnectionInfo = _remoteRunspaceInfo.Runspace.ConnectionInfo as WSManConnectionInfo;
if (wsmanConnectionInfo == null)
if (!(_remoteRunspaceInfo.Runspace.ConnectionInfo is WSManConnectionInfo wsmanConnectionInfo))
{
return string.Empty;
}
Expand All @@ -2650,8 +2649,7 @@ private string GenerateAuthenticationMechanismParameter()
return string.Empty;
}

WSManConnectionInfo wsmanConnectionInfo = _remoteRunspaceInfo.Runspace.ConnectionInfo as WSManConnectionInfo;
if (wsmanConnectionInfo == null)
if (!(_remoteRunspaceInfo.Runspace.ConnectionInfo is WSManConnectionInfo wsmanConnectionInfo))
{
return string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ internal CultureInfo Culture
/// <returns>True if both the objects are same or else returns false.</returns>
public override bool Equals(object inputObject)
{
ObjectCommandPropertyValue objectCommandPropertyValueObject = inputObject as ObjectCommandPropertyValue;
if (objectCommandPropertyValueObject == null)
if (!(inputObject is ObjectCommandPropertyValue objectCommandPropertyValueObject))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ internal static string[] GetDefaultKeyPropertySet(PSObject mshObj)
return null;
}

PSPropertySet defaultKeys = standardNames.Members["DefaultKeyPropertySet"] as PSPropertySet;

if (defaultKeys == null)
if (!(standardNames.Members["DefaultKeyPropertySet"] is PSPropertySet defaultKeys))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,7 @@ private static object ProcessValue(object obj, int currentDepth, in ConvertToJso
/// </returns>
private static object AddPsProperties(object psObj, object obj, int depth, bool isPurePSObj, bool isCustomObj, in ConvertToJsonContext context)
{
PSObject pso = psObj as PSObject;

if (pso == null)
if (!(psObj is PSObject pso))
{
return obj;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Microsoft.PowerShell.Security/security/AclCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ public static string GetOwner(PSObject instance)
throw PSTraceSource.NewArgumentNullException(nameof(instance));
}

ObjectSecurity sd = instance.BaseObject as ObjectSecurity;
if (sd == null)
if (!(instance.BaseObject is ObjectSecurity sd))
{
throw PSTraceSource.NewArgumentNullException(nameof(instance));
}
Expand Down Expand Up @@ -248,8 +247,7 @@ public static string GetGroup(PSObject instance)
throw PSTraceSource.NewArgumentNullException(nameof(instance));
}

ObjectSecurity sd = instance.BaseObject as ObjectSecurity;
if (sd == null)
if (!(instance.BaseObject is ObjectSecurity sd))
{
throw PSTraceSource.NewArgumentNullException(nameof(instance));
}
Expand Down Expand Up @@ -588,8 +586,7 @@ public static string GetSddl(PSObject instance)
throw PSTraceSource.NewArgumentNullException(nameof(instance));
}

ObjectSecurity sd = instance.BaseObject as ObjectSecurity;
if (sd == null)
if (!(instance.BaseObject is ObjectSecurity sd))
{
throw PSTraceSource.NewArgumentNullException(nameof(instance));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,7 @@ internal static AppliesTo GetAllApplicableTypes(TypeInfoDataBase db, AppliesTo a
else
{
// check if we have a type group reference
TypeGroupReference tgr = r as TypeGroupReference;

if (tgr == null)
if (!(r is TypeGroupReference tgr))
continue;

// find the type group definition the reference points to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ fid is GroupEndData ||
return false;
}

string classId = GetProperty(so, FormatInfoData.classidProperty) as string;

if (classId == null)
if (!(GetProperty(so, FormatInfoData.classidProperty) is string classId))
{
// it's not one of the objects derived from FormatInfoData
return false;
Expand Down Expand Up @@ -113,9 +111,7 @@ fid is GroupEndData ||
return so;
}

string classId = GetProperty(so, FormatInfoData.classidProperty) as string;

if (classId == null)
if (!(GetProperty(so, FormatInfoData.classidProperty) is string classId))
{
// it's not one of the objects derived from FormatInfoData,
// just return it as is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ private List<CimClass> GetInheritanceChain(CimInstance cimInstance)
/// <returns></returns>
public override Collection<string> GetTypeNameHierarchy(object baseObject)
{
var cimInstance = baseObject as CimInstance;
if (cimInstance == null)
if (!(baseObject is CimInstance cimInstance))
{
throw new ArgumentNullException(nameof(baseObject));
}
Expand Down Expand Up @@ -362,8 +361,7 @@ public override bool IsSettable(PSAdaptedProperty adaptedProperty)
return false;
}

CimProperty cimProperty = adaptedProperty.Tag as CimProperty;
if (cimProperty == null)
if (!(adaptedProperty.Tag is CimProperty cimProperty))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ internal object Transform(EngineIntrinsics engineIntrinsics, object inputData, b
else
temp = result;

PSReference reference = temp as PSReference;

if (reference == null)
if (!(temp is PSReference reference))
{
throw new PSInvalidCastException("InvalidCastExceptionReferenceTypeExpected", null,
ExtendedTypeSystem.ReferenceTypeExpected);
Expand Down
6 changes: 2 additions & 4 deletions src/System.Management.Automation/engine/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,7 @@ public sealed class ValidateLengthAttribute : ValidateEnumeratedArgumentsAttribu
/// <exception cref="ArgumentException">For invalid arguments.</exception>
protected override void ValidateElement(object element)
{
string objectString = element as string;
if (objectString == null)
if (!(element is string objectString))
{
throw new ValidationMetadataException(
"ValidateLengthNotString",
Expand Down Expand Up @@ -1857,8 +1856,7 @@ protected override void Validate(object arguments, EngineIntrinsics engineIntrin
Metadata.ValidateNotNullFailure);
}

var path = arguments as string;
if (path == null)
if (!(arguments is string path))
{
throw new ValidationMetadataException(
"PathArgumentIsNotValid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ private Dictionary<MergedCompiledCommandParameter, object> GetDefaultParameterVa

foreach (DictionaryEntry entry in DefaultParameterValues)
{
string key = entry.Key as string;
if (key == null)
if (!(entry.Key is string key))
{
continue;
}
Expand Down Expand Up @@ -4389,8 +4388,7 @@ public override bool ContainsKey(object key)
throw PSTraceSource.NewArgumentNullException(nameof(key));
}

var strKey = key as string;
if (strKey == null) { return false; }
if (!(key is string strKey)) { return false; }

string keyAfterTrim = strKey.Trim();
return base.ContainsKey(keyAfterTrim);
Expand All @@ -4416,8 +4414,7 @@ private void AddImpl(object key, object value, bool isSelfIndexing)
throw PSTraceSource.NewArgumentNullException(nameof(key));
}

var strKey = key as string;
if (strKey == null)
if (!(key is string strKey))
{
throw PSTraceSource.NewArgumentException(nameof(key), ParameterBinderStrings.StringValueKeyExpected, key, key.GetType().FullName);
}
Expand Down Expand Up @@ -4462,8 +4459,7 @@ public override object this[object key]
{
if (key == null) { throw PSTraceSource.NewArgumentNullException(nameof(key)); }

var strKey = key as string;
if (strKey == null) { return null; }
if (!(key is string strKey)) { return null; }

string keyAfterTrim = strKey.Trim();
return base[keyAfterTrim];
Expand All @@ -4486,8 +4482,7 @@ public override void Remove(object key)
throw PSTraceSource.NewArgumentNullException(nameof(key));
}

var strKey = key as string;
if (strKey == null) { return; }
if (!(key is string strKey)) { return; }

string keyAfterTrim = strKey.Trim();
if (base.ContainsKey(keyAfterTrim))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ private static bool CompleteAgainstSwitchFile(Ast lastAst, Token tokenBeforeCurs
if (lastAst.Parent is CommandExpressionAst)
{
// Handle "switch -file m<tab>" or "switch -file *.ps1<tab>"
var pipeline = lastAst.Parent.Parent as PipelineAst;
if (pipeline == null)
if (!(lastAst.Parent.Parent is PipelineAst pipeline))
{
return false;
}
Expand Down
Loading