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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,12 @@ public void AppendLicensesToDictionary(IDictionary<string, string> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public virtual async Task<ScanResult> 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;
}
Expand Down