From 0b9921889f2876da74d07921809ec1f4f5b5649c Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sat, 29 Mar 2025 08:37:13 +0200 Subject: [PATCH] Fix dictionary usage related analyzer problems --- Directory.Build.props | 3 --- main/HPSF/Section.cs | 2 +- main/HSSF/Record/RecordFactory.cs | 4 ++-- main/POIFS/FileSystem/EntryUtils.cs | 4 ++-- main/POIFS/FileSystem/FilteringDirectoryNode.cs | 13 +++++++------ ooxml/POIXMLDocumentPart.cs | 6 +++--- ooxml/XSSF/UserModel/XSSFSheet.cs | 12 ++++++------ ooxml/XSSF/UserModel/XSSFWorkbook.cs | 10 +++++++--- openxml4Net/OPC/Internal/ContentTypeManager.cs | 3 +-- 9 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index dd5d8c50a..917e5b595 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -88,17 +88,14 @@ $(NoWarn);CA1830 $(NoWarn);CA1846 $(NoWarn);CA1834 - $(NoWarn);CA1841 $(NoWarn);CA1845 $(NoWarn);CA1847 $(NoWarn);CA1850 $(NoWarn);CA1852 - $(NoWarn);CA1854 $(NoWarn);CA1859 $(NoWarn);CA1861 $(NoWarn);CA1862 $(NoWarn);CA1864 - $(NoWarn);CA1853 $(NoWarn);CA1872 $(NoWarn);CA2019 $(NoWarn);CA2022 diff --git a/main/HPSF/Section.cs b/main/HPSF/Section.cs index 1ce255b60..a712ee875 100644 --- a/main/HPSF/Section.cs +++ b/main/HPSF/Section.cs @@ -437,7 +437,7 @@ public void SetProperty(int id, long variantType, Object value) /// @see Variant public void SetProperty(Property p) { - Property old = properties.ContainsKey(p.ID)? properties[p.ID] : null; + Property old = properties.TryGetValue(p.ID, out Property value) ? value : null; if(old == null || !old.Equals(p)) { properties[p.ID] = p; diff --git a/main/HSSF/Record/RecordFactory.cs b/main/HSSF/Record/RecordFactory.cs index 8626b1649..5ec76bdcc 100644 --- a/main/HSSF/Record/RecordFactory.cs +++ b/main/HSSF/Record/RecordFactory.cs @@ -614,9 +614,9 @@ private static Dictionary RecordsToMap(Type[] records) throw new RecordFormatException( "Unable to determine record types", ArgumentException); } - if (result.ContainsKey(sid)) + if (result.TryGetValue(sid, out I_RecordCreator value)) { - Type prevClass = result[sid].GetRecordClass(); + Type prevClass = value.GetRecordClass(); throw new RuntimeException("duplicate record sid 0x" + sid.ToString("X", CultureInfo.CurrentCulture) + " for classes (" + recClass.Name + ") and (" + prevClass.Name + ")"); } diff --git a/main/POIFS/FileSystem/EntryUtils.cs b/main/POIFS/FileSystem/EntryUtils.cs index ebbbc137e..d326d9b55 100644 --- a/main/POIFS/FileSystem/EntryUtils.cs +++ b/main/POIFS/FileSystem/EntryUtils.cs @@ -213,7 +213,7 @@ public static bool AreDirectoriesIdentical(DirectoryEntry dirA, DirectoryEntry d foreach (Entry b in dirB) { String bName = b.Name; - if (!aSizes.ContainsKey(bName)) + if (!aSizes.TryGetValue(bName, out int value)) { // In B but not A return false; @@ -228,7 +228,7 @@ public static bool AreDirectoriesIdentical(DirectoryEntry dirA, DirectoryEntry d { size = ((DocumentNode)b).Size; } - if (size != aSizes[(bName)]) + if (size != value) { // Either the wrong type, or they're different sizes return false; diff --git a/main/POIFS/FileSystem/FilteringDirectoryNode.cs b/main/POIFS/FileSystem/FilteringDirectoryNode.cs index 3941f3069..5e5c6b655 100644 --- a/main/POIFS/FileSystem/FilteringDirectoryNode.cs +++ b/main/POIFS/FileSystem/FilteringDirectoryNode.cs @@ -58,11 +58,13 @@ public FilteringDirectoryNode(DirectoryEntry directory, ICollection excl // Applies to a child String child = excl.Substring(0, splitAt); String childExcl = excl.Substring(splitAt + 1); - if (!this.childExcludes.ContainsKey(child)) + if (!this.childExcludes.TryGetValue(child, out List value)) { - this.childExcludes.Add(child, new List()); + value = new List(); + this.childExcludes.Add(child, value); } - this.childExcludes[child].Add(childExcl); + + value.Add(childExcl); } } } @@ -134,10 +136,9 @@ public Entry GetEntry(String name) private Entry WrapEntry(Entry entry) { String name = entry.Name; - if (childExcludes.ContainsKey(name) && entry is DirectoryEntry directoryEntry) + if (childExcludes.TryGetValue(name, out List value) && entry is DirectoryEntry directoryEntry) { - return new FilteringDirectoryNode( - directoryEntry, childExcludes[name]); + return new FilteringDirectoryNode(directoryEntry, value); } return entry; } diff --git a/ooxml/POIXMLDocumentPart.cs b/ooxml/POIXMLDocumentPart.cs index 1b7826fa3..04a5a83d2 100644 --- a/ooxml/POIXMLDocumentPart.cs +++ b/ooxml/POIXMLDocumentPart.cs @@ -716,14 +716,14 @@ protected RelationPart CreateRelationship(POIXMLRelation descriptor, POIXMLFacto public TValue PutDictionary(Dictionary dict, TKey key, TValue value) { - TValue oldValue = default(TValue); - if (dict.ContainsKey(key)) + if (dict.TryGetValue(key, out TValue oldValue)) { - oldValue = dict[key]; dict[key] = value; } else + { dict.Add(key, value); + } return oldValue; } diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index 7aeece652..64e17fcb5 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -4438,22 +4438,22 @@ public void AddIgnoredErrors(CellRangeAddress region, params IgnoredErrorType[] /// Map of error type to the range(s) where they are ignored. public Dictionary> GetIgnoredErrors() { - Dictionary> result = - new Dictionary>(); + Dictionary> result = new(); if(worksheet.IsSetIgnoredErrors()) { foreach(CT_IgnoredError err in worksheet.ignoredErrors.ignoredError) { foreach(IgnoredErrorType errType in GetErrorTypes(err)) { - if(!result.ContainsKey(errType)) + if(!result.TryGetValue(errType, out ISet value)) { - result.Add(errType, new HashSet()); + value = new HashSet(); + result.Add(errType, value); } - foreach(object ref1 in err.sqref) + foreach(string ref1 in err.sqref) { - result[errType].Add(CellRangeAddress.ValueOf(ref1.ToString())); + value.Add(CellRangeAddress.ValueOf(ref1)); } } } diff --git a/ooxml/XSSF/UserModel/XSSFWorkbook.cs b/ooxml/XSSF/UserModel/XSSFWorkbook.cs index 000e61cc8..ef4bb4f49 100644 --- a/ooxml/XSSF/UserModel/XSSFWorkbook.cs +++ b/ooxml/XSSF/UserModel/XSSFWorkbook.cs @@ -802,10 +802,14 @@ public IName CreateName() private void PutValuesMapping(string key, XSSFName name) { - if (namedRangesByName.ContainsKey(key)) - namedRangesByName[key].Add(name); + if(namedRangesByName.TryGetValue(key, out List value)) + { + value.Add(name); + } else - namedRangesByName.Add(key, new List() { name }); + { + namedRangesByName.Add(key, [name]); + } } private XSSFName CreateAndStoreName(CT_DefinedName ctName) diff --git a/openxml4Net/OPC/Internal/ContentTypeManager.cs b/openxml4Net/OPC/Internal/ContentTypeManager.cs index 55e69b621..6fc7e7508 100644 --- a/openxml4Net/OPC/Internal/ContentTypeManager.cs +++ b/openxml4Net/OPC/Internal/ContentTypeManager.cs @@ -269,8 +269,7 @@ public bool IsContentTypeRegister(String contentType) if (contentType == null) throw new ArgumentException("contentType"); - return (this.defaultContentType.Values.Contains(contentType) || (this.overrideContentType != null && this.overrideContentType - .Values.Contains(contentType))); + return defaultContentType.ContainsValue(contentType) || (overrideContentType != null && overrideContentType.ContainsValue(contentType)); } /**