Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[StyleCleanUp] Address IDE warnings Part 2 #10170

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions src/Microsoft.DotNet.Wpf/src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ dotnet_diagnostic.IDE0005.severity = suggestion
# IDE0017: Simplify object initialization
dotnet_diagnostic.IDE0017.severity = suggestion

# IDE0019: Use pattern matching to avoid as followed by a null check
dotnet_diagnostic.IDE0019.severity = suggestion

# IDE0020: Use pattern matching to avoid is check followed by a cast (with variable)
dotnet_diagnostic.IDE0020.severity = suggestion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,9 @@ internal void ProcessDefinitionNamespace(XamlDefTagNode xamlDefTagNode)
case XmlNodeType.CDATA:
case XmlNodeType.Text:
{
IXmlLineInfo xmlLineInfo = xmlReader as IXmlLineInfo;
int lineNumber = 0;
int lineNumber = 0;

if (null != xmlLineInfo)
if (xmlReader is IXmlLineInfo xmlLineInfo)
harshit7962 marked this conversation as resolved.
Show resolved Hide resolved
{
lineNumber = xmlLineInfo.LineNumber;
}
Expand Down Expand Up @@ -1306,8 +1305,7 @@ private void InitializeReflectionHelper()
{
for (int i = 0; i < ReferenceAssemblyList.Count; i++)
{
ReferenceAssembly refasm = ReferenceAssemblyList[i] as ReferenceAssembly;
if (refasm != null && refasm.Path.Length > 0)
if (ReferenceAssemblyList[i] is ReferenceAssembly refasm && refasm.Path.Length > 0)
{
paths.Add(refasm.Path);
}
Expand All @@ -1324,9 +1322,7 @@ private void InitializeTypeMapper()
{
for (int i = 0; i < ReferenceAssemblyList.Count; i++)
{
ReferenceAssembly refasm = ReferenceAssemblyList[i] as ReferenceAssembly;

if (refasm != null && refasm.Path.Length > 0)
if (ReferenceAssemblyList[i] is ReferenceAssembly refasm && refasm.Path.Length > 0)
{
_typeMapper.SetAssemblyPath(refasm.AssemblyName, refasm.Path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ internal bool DoCompilation(string assemblyName, string language, string rootNam
{
for (int i = 0; i < _mc.ReferenceAssemblyList.Count; i++)
{
ReferenceAssembly asmReference = _mc.ReferenceAssemblyList[i] as ReferenceAssembly;

if (asmReference != null)
if (_mc.ReferenceAssemblyList[i] is ReferenceAssembly asmReference)
{
if (string.Equals(asmReference.AssemblyName, assemblyName, StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public byte[] GetChecksum(string fileName, Guid hashGuid)
if (HostFileManager != null)
{
object docData = HostFileManager.GetFileDocData(fileName);
IPersistFileCheckSum fileChecksummer = docData as IPersistFileCheckSum;
if (fileChecksummer != null)
if (docData is IPersistFileCheckSum fileChecksummer)
{
byte[] tempBytes = new byte[1024];
int actualSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,7 @@ private void RemoveEntityByName(XmlDocument xmlProjectDoc, string sItemName, str

for (int i = 0; i < root.ChildNodes.Count; i++)
{
XmlElement nodeGroup = root.ChildNodes[i] as XmlElement;

if (nodeGroup != null && string.Equals(nodeGroup.Name, groupName, StringComparison.OrdinalIgnoreCase))
if (root.ChildNodes[i] is XmlElement nodeGroup && string.Equals(nodeGroup.Name, groupName, StringComparison.OrdinalIgnoreCase))
{
//
// This is ItemGroup element.
Expand All @@ -644,9 +642,7 @@ private void RemoveEntityByName(XmlDocument xmlProjectDoc, string sItemName, str

for (int j = 0; j < nodeGroup.ChildNodes.Count; j++)
{
XmlElement nodeItem = nodeGroup.ChildNodes[j] as XmlElement;

if (nodeItem != null && string.Equals(nodeItem.Name, sItemName, StringComparison.OrdinalIgnoreCase))
if (nodeGroup.ChildNodes[j] is XmlElement nodeItem && string.Equals(nodeItem.Name, sItemName, StringComparison.OrdinalIgnoreCase))
{
// This is the item that need to remove.
// Add it into the temporary array list.
Expand All @@ -663,13 +659,11 @@ private void RemoveEntityByName(XmlDocument xmlProjectDoc, string sItemName, str
{
foreach (object node in itemToRemove)
{
XmlElement item = node as XmlElement;

//
// Remove this item from its parent node.
// the parent node should be nodeGroup.
//
if (item != null)
if (node is XmlElement item)
{
nodeGroup.RemoveChild(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,14 +1123,13 @@ internal void LoadAssemblyInfoRecord(BamlAssemblyInfoRecord record)
internal void EnsureAssemblyRecord(Assembly asm)
{
string fullName = asm.FullName;
BamlAssemblyInfoRecord record = ObjectHashTable[fullName] as BamlAssemblyInfoRecord;

// If we don't have an assembly record for this assembly yet it is most likely
// because it is an assembly that is part of the default namespace and was not defined
// using a mapping PI. In that case, add an assembly record to the object cache and
// populate it with the required data. Note that it DOES NOT have a valid AssemblyId
// and this is not written out the the baml stream.
if (record == null)
if (ObjectHashTable[fullName] is not BamlAssemblyInfoRecord record)
{
record = new BamlAssemblyInfoRecord();
record.AssemblyFullName = fullName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,7 @@ private IBamlDictionaryKey FindBamlDictionaryKey(KeyDeferRecord record)
for (int i = 0; i < record.RecordList.Count; i++)
{
ValueDeferRecord valueDeferRecord = (ValueDeferRecord)record.RecordList[i];
IBamlDictionaryKey dictionaryKey = valueDeferRecord.Record as IBamlDictionaryKey;
if (dictionaryKey != null)
if (valueDeferRecord.Record is IBamlDictionaryKey dictionaryKey)
{
return dictionaryKey;
}
Expand Down Expand Up @@ -567,11 +566,10 @@ internal void WriteDefAttribute(XamlDefAttributeNode xamlDefNode)
// attribute is *NOT* a MarkupExtension. A MarkupExtension would cause
// WriteKeyElementStart being called.
KeyDeferRecord keyRecord = (KeyDeferRecord)(_deferKeys[_deferKeys.Count-1]);
BamlDefAttributeKeyStringRecord defKeyRecord = keyRecord.Record as BamlDefAttributeKeyStringRecord;
if (defKeyRecord == null)
if (keyRecord.Record is not BamlDefAttributeKeyStringRecord defKeyRecord)
{
defKeyRecord =
(BamlDefAttributeKeyStringRecord) BamlRecordManager.GetWriteRecord(BamlRecordType.DefAttributeKeyString);
(BamlDefAttributeKeyStringRecord)BamlRecordManager.GetWriteRecord(BamlRecordType.DefAttributeKeyString);
TransferOldSharedData(keyRecord.Record as IBamlDictionaryKey, defKeyRecord as IBamlDictionaryKey);
keyRecord.Record = defKeyRecord;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4710,8 +4710,7 @@ internal void SetPropertyMember (object propertyMember)
else
{
// Cache a additional MemberInfo for the given attribute
object[] arr = PropertyMember as object[];
if (arr == null)
if (PropertyMember is not object[] arr)
{
arr = new object[3];
arr[0] = PropertyMember;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,25 +745,23 @@ public override void WriteElementStart(XamlElementStartNode xamlElementStartNode

if (xamlElementStartNode.SerializerType != null && _styleModeStack.Depth > 0)
{
XamlSerializer serializer = XamlTypeMapper.CreateInstance(xamlElementStartNode.SerializerType)
as XamlSerializer;
if (serializer == null)
{
ThrowException(nameof(SR.ParserNoSerializer),
xamlElementStartNode.TypeFullName,
xamlElementStartNode.LineNumber,
xamlElementStartNode.LinePosition);
}
else
{
// Depending on whether this is the compile case or the parse xaml
// case, we want to convert the xaml into baml or objects.

#if PBTCOMPILER
serializer.ConvertXamlToBaml(TokenReader,
BamlRecordWriter == null ? ParserContext : BamlRecordWriter.ParserContext,
xamlElementStartNode, BamlRecordWriter);
#else
if (XamlTypeMapper.CreateInstance(xamlElementStartNode.SerializerType) is not XamlSerializer serializer)
{
ThrowException(nameof(SR.ParserNoSerializer),
xamlElementStartNode.TypeFullName,
xamlElementStartNode.LineNumber,
xamlElementStartNode.LinePosition);
}
else
{
// Depending on whether this is the compile case or the parse xaml
// case, we want to convert the xaml into baml or objects.

#if PBTCOMPILER
serializer.ConvertXamlToBaml(TokenReader,
BamlRecordWriter == null ? ParserContext : BamlRecordWriter.ParserContext,
xamlElementStartNode, BamlRecordWriter);
#else

// If we're in the content of the template, we'll convert to baml. Then TemplateBamlRecordReader
// gets the option of instantiating it or keeping it in baml. For example, if this is a nested
Expand Down Expand Up @@ -803,9 +801,9 @@ public override void WriteElementStart(XamlElementStartNode xamlElementStartNode
TreeBuilder.RecordReader);
}

#endif
#endif

}
}

}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1467,8 +1467,7 @@ public Type ContextDataType
{
get
{
DictionaryContextData dcd = _contextData as DictionaryContextData;
if (dcd == null)
if (_contextData is not DictionaryContextData dcd)
{
return _contextData as Type;
}
Expand Down Expand Up @@ -3125,8 +3124,7 @@ private void WriteDefAttributes(
ThrowException(nameof(SR.ParserNoDictionaryName));
}

DictionaryContextData dictionaryData = ParentContext.ContextData as DictionaryContextData;
if (dictionaryData != null)
if (ParentContext.ContextData is DictionaryContextData dictionaryData)
{
object key;
// Note that not all keys can be resolved at compile time. For those that fail,
Expand Down Expand Up @@ -3952,13 +3950,12 @@ private void CompileComplexProperty(
// BamlRecordReader has secondary protection against nested property
// records, but the error message less friendly to users. ("'Property'
// record unexpected in BAML stream.")
ElementContextStackData parentTag = ElementContextStack.ParentContext as ElementContextStackData;
if( parentTag != null )
if (ElementContextStack.ParentContext is ElementContextStackData parentTag)
{
if ( parentTag.ContextType == ElementContextType.PropertyComplex ||
if (parentTag.ContextType == ElementContextType.PropertyComplex ||
parentTag.ContextType == ElementContextType.PropertyArray ||
parentTag.ContextType == ElementContextType.PropertyIList ||
parentTag.ContextType == ElementContextType.PropertyIDictionary )
parentTag.ContextType == ElementContextType.PropertyIDictionary)
{
ThrowException(nameof(SR.ParserNestedComplexProp), complexPropName);
}
Expand Down Expand Up @@ -5263,21 +5260,18 @@ bool Normalization
{
set
{

Debug.Assert(null != XmlReader, "XmlReader is not yet set");
//check if it's a XmlCompatibilityReader first
XmlCompatibilityReader xmlCompatReader = XmlReader as XmlCompatibilityReader;
if (null != xmlCompatReader)
if (XmlReader is XmlCompatibilityReader xmlCompatReader)
{
xmlCompatReader.Normalization = true;
}
else
{
//now check for XmlTextReader
XmlTextReader xmlTextReader = XmlReader as XmlTextReader;

// review, what if not the XmlTextReader.
if (null != xmlTextReader)
if (XmlReader is XmlTextReader xmlTextReader)
{
xmlTextReader.Normalization = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3891,20 +3891,16 @@ internal void ResetMapper()
_linePosition = 0;
_isProtectedAttributeAllowed = false;

NamespaceMapEntry[] defaultNsMaps = _namespaceMapHashList[XamlReaderHelper.DefaultNamespaceURI] as NamespaceMapEntry[];
NamespaceMapEntry[] definitionNsMaps = _namespaceMapHashList[XamlReaderHelper.DefinitionNamespaceURI] as NamespaceMapEntry[];
NamespaceMapEntry[] definitionMetroNsMaps = _namespaceMapHashList[XamlReaderHelper.DefinitionMetroNamespaceURI] as NamespaceMapEntry[];

_namespaceMapHashList.Clear();
if (null != defaultNsMaps)
if (_namespaceMapHashList[XamlReaderHelper.DefaultNamespaceURI] is NamespaceMapEntry[] defaultNsMaps)
{
_namespaceMapHashList.Add(XamlReaderHelper.DefaultNamespaceURI, defaultNsMaps);
}
if (null != definitionNsMaps)
if (_namespaceMapHashList[XamlReaderHelper.DefinitionNamespaceURI] is NamespaceMapEntry[] definitionNsMaps)
{
_namespaceMapHashList.Add(XamlReaderHelper.DefinitionNamespaceURI, definitionNsMaps);
}
if (null != definitionMetroNsMaps)
if (_namespaceMapHashList[XamlReaderHelper.DefinitionMetroNamespaceURI] is NamespaceMapEntry[] definitionMetroNsMaps)
{
_namespaceMapHashList.Add(XamlReaderHelper.DefinitionMetroNamespaceURI, definitionMetroNsMaps);
}
Expand Down Expand Up @@ -4012,8 +4008,7 @@ internal void SetPropertyAndType(
"GetPropertyAndType must always be called before SetPropertyAndType");

// add the type taking a lock
PropertyAndType pAndT = _dpLookupHashtable[dpName] as PropertyAndType;
if (pAndT == null)
if (_dpLookupHashtable[dpName] is not PropertyAndType pAndT)
{
_dpLookupHashtable[dpName] = new PropertyAndType(null, dpInfo, false, true, ownerType, isInternal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ IEnumerator IEnumerable.GetEnumerator()
/// <param name="index">The zero-based index in array at which copying begins</param>
public void CopyTo(Array array, int index)
{
IDictionary dict = GetNamespacesInScope(NamespaceScope.All) as IDictionary;
if (dict != null)
dict.CopyTo(array,index);
if (GetNamespacesInScope(NamespaceScope.All) is IDictionary dict)
dict.CopyTo(array, index);
}

#endregion ICollectionMethods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,14 @@ private static bool IsDialogOverrideEnabled
if (key != null)
{
object dbgJITDebugLaunchSettingValue = key.GetValue("DbgJITDebugLaunchSetting");
string dbgManagedDebuggerValue = key.GetValue("DbgManagedDebugger") as string;

//
// Only count the enable if there's a JIT debugger to launch.
//
enabled = (dbgJITDebugLaunchSettingValue is int && ((int)dbgJITDebugLaunchSettingValue & 2) != 0);
if (enabled)
{
enabled = dbgManagedDebuggerValue != null && dbgManagedDebuggerValue.Length > 0;
enabled = key.GetValue("DbgManagedDebugger") is string dbgManagedDebuggerValue && dbgManagedDebuggerValue.Length > 0;
}
}
return enabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ static void CleanupCollectedAssemblies(object state) // dummy parameter required
{
foreach (object key in _assemblies.Keys)
{
WeakReference weakRef = key as WeakReference;
if (weakRef == null)
if (key is not WeakReference weakRef)
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,7 @@ private object DispatcherCallbackOperation(object o)
param.retVal = IntPtr.Zero;
if (_bond == Bond.Attached)
{
HwndWrapperHook hook= _hook.Target as HwndWrapperHook;

if (hook != null)
if (_hook.Target is HwndWrapperHook hook)
{
// make the call
param.retVal = hook(param.hwnd, param.msg, param.wParam, param.lParam, ref param.handled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public override string ToString()

public override bool Equals( object obj )
{
XFORM xform = obj as XFORM;

if( xform == null )
if (obj is not XFORM xform)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ internal static Type GetReflectionType(object item)
if (item == null)
return null;

ICustomTypeProvider ictp = item as ICustomTypeProvider;
if (ictp == null)
if (item is not ICustomTypeProvider ictp)
return item.GetType();
else
return ictp.GetCustomType();
Expand Down
Loading