diff --git a/.editorconfig b/.editorconfig index 95340fb0b..b420e6dfa 100644 --- a/.editorconfig +++ b/.editorconfig @@ -836,7 +836,7 @@ dotnet_diagnostic.CA1852.severity = suggestion # https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1854 # CA1854: Prefer 'TryGetValue' over 'ContainsKey' and 'Item' when accessing dictionary items -dotnet_diagnostic.CA1854.severity = suggestion +dotnet_diagnostic.CA1854.severity = error # https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1860 # CA1860: Avoid using 'Enumerable.Any()' extension method. diff --git a/src/Microsoft.Sbom.Api/Executors/ComponentDetectionBaseWalker.cs b/src/Microsoft.Sbom.Api/Executors/ComponentDetectionBaseWalker.cs index 1ee767bf3..997abe723 100644 --- a/src/Microsoft.Sbom.Api/Executors/ComponentDetectionBaseWalker.cs +++ b/src/Microsoft.Sbom.Api/Executors/ComponentDetectionBaseWalker.cs @@ -179,15 +179,15 @@ async Task Scan(string path) extendedComponent = new ExtendedScannedComponent(scannedComponent); } - if (LicenseDictionary != null && LicenseDictionary.ContainsKey($"{componentName}@{componentVersion}")) + if (LicenseDictionary != null && LicenseDictionary.TryGetValue($"{componentName}@{componentVersion}", out var licenseConcluded)) { - extendedComponent.LicenseConcluded = LicenseDictionary[$"{componentName}@{componentVersion}"]; + extendedComponent.LicenseConcluded = licenseConcluded; } - if (packageDetailsDictionary != null && packageDetailsDictionary.ContainsKey((componentName, componentVersion))) + if (packageDetailsDictionary != null && packageDetailsDictionary.TryGetValue((componentName, componentVersion), out var packageDetails)) { - extendedComponent.Supplier = string.IsNullOrEmpty(packageDetailsDictionary[(componentName, componentVersion)].Supplier) ? null : packageDetailsDictionary[(componentName, componentVersion)].Supplier; - extendedComponent.LicenseDeclared = string.IsNullOrEmpty(packageDetailsDictionary[(componentName, componentVersion)].License) ? null : packageDetailsDictionary[(componentName, componentVersion)].License; + extendedComponent.Supplier = string.IsNullOrEmpty(packageDetails.Supplier) ? null : packageDetails.Supplier; + extendedComponent.LicenseDeclared = string.IsNullOrEmpty(packageDetails.License) ? null : packageDetails.License; } await output.Writer.WriteAsync(extendedComponent); diff --git a/src/Microsoft.Sbom.Api/Executors/LicenseInformationFetcher.cs b/src/Microsoft.Sbom.Api/Executors/LicenseInformationFetcher.cs index 3ebaed640..04c8ed9d6 100644 --- a/src/Microsoft.Sbom.Api/Executors/LicenseInformationFetcher.cs +++ b/src/Microsoft.Sbom.Api/Executors/LicenseInformationFetcher.cs @@ -152,14 +152,12 @@ public void AppendLicensesToDictionary(IDictionary partialLicens public string GetFromLicenseDictionary(string key) { - var value = string.Empty; - - if (licenseDictionary.ContainsKey(key)) + if (licenseDictionary.TryGetValue(key, out var value)) { - licenseDictionary.TryGetValue(key, out value); + return value; } - return value; + return string.Empty; } private bool IsUndefinedLicense(string license) diff --git a/src/Microsoft.Sbom.Api/Manifest/Configuration/SbomConfigProvider.cs b/src/Microsoft.Sbom.Api/Manifest/Configuration/SbomConfigProvider.cs index 089c8133c..ce3af79f3 100644 --- a/src/Microsoft.Sbom.Api/Manifest/Configuration/SbomConfigProvider.cs +++ b/src/Microsoft.Sbom.Api/Manifest/Configuration/SbomConfigProvider.cs @@ -158,14 +158,12 @@ public object GetMetadata(MetadataKey key) public bool TryGetMetadata(MetadataKey key, out object value) { - if (MetadataDictionary.ContainsKey(key)) + if (MetadataDictionary.TryGetValue(key, out value)) { logger.Debug($"Found value for header {key} in internal metadata."); - value = MetadataDictionary[key]; return true; } - value = null; return false; } diff --git a/src/Microsoft.Sbom.Api/Utils/ComponentDetectionCliArgumentBuilder.cs b/src/Microsoft.Sbom.Api/Utils/ComponentDetectionCliArgumentBuilder.cs index f694339b5..2cbd7434b 100644 --- a/src/Microsoft.Sbom.Api/Utils/ComponentDetectionCliArgumentBuilder.cs +++ b/src/Microsoft.Sbom.Api/Utils/ComponentDetectionCliArgumentBuilder.cs @@ -182,9 +182,9 @@ public ComponentDetectionCliArgumentBuilder AddArg(string name, string value) } // Check if a key already exists for the --DirectoryExclusionList, if so, check that the value isn't a duplicate. If these conditions are true then append the new value delimited by a semicolon. - if (keyValueArgs.ContainsKey(name) && !keyValueArgs.ContainsValue(value) && name.Equals(DirectoryExclusionListParamName, StringComparison.OrdinalIgnoreCase)) + if (keyValueArgs.TryGetValue(name, out var argValue) && !keyValueArgs.ContainsValue(value) && name.Equals(DirectoryExclusionListParamName, StringComparison.OrdinalIgnoreCase)) { - keyValueArgs[name] = $"{keyValueArgs[name]};{value}"; + keyValueArgs[name] = $"{argValue};{value}"; return this; } diff --git a/src/Microsoft.Sbom.Api/Utils/ComponentDetectorCachedExecutor.cs b/src/Microsoft.Sbom.Api/Utils/ComponentDetectorCachedExecutor.cs index 5af88ee01..3e421a9d4 100644 --- a/src/Microsoft.Sbom.Api/Utils/ComponentDetectorCachedExecutor.cs +++ b/src/Microsoft.Sbom.Api/Utils/ComponentDetectorCachedExecutor.cs @@ -42,13 +42,13 @@ public virtual async Task ScanAsync(ScanSettings args) var scanSettingsHash = args.ToString().GetHashCode(); - if (results.ContainsKey(scanSettingsHash)) + if (results.TryGetValue(scanSettingsHash, out var result)) { log.Debug("Using cached CD scan result for the call with the same arguments"); - return results[scanSettingsHash]; + return result; } - var result = await detector.ScanAsync(args); + result = await detector.ScanAsync(args); results.TryAdd(scanSettingsHash, result); return result; }