Skip to content

Commit

Permalink
refactor: multiple roslyn analyzer fixes (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Sep 25, 2023
1 parent a8dea41 commit 4c9ec94
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 30 deletions.
12 changes: 0 additions & 12 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1031,14 +1031,6 @@ dotnet_diagnostic.IDE0090.severity = suggestion
# IDE0110: Discard can be removed
dotnet_diagnostic.IDE0110.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0120
# IDE0120: Simplify LINQ expression
dotnet_diagnostic.IDE0120.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0200
# IDE0200: Lambda expression can be simplified
dotnet_diagnostic.IDE0200.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0220
# IDE0220: 'foreach' statement implicitly converts '...' to '...'
dotnet_diagnostic.IDE0220.severity = suggestion
Expand All @@ -1047,10 +1039,6 @@ dotnet_diagnostic.IDE0220.severity = suggestion
# IDE0251: Member can be made 'readonly'
dotnet_diagnostic.IDE0251.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0270
# IDE0270: Null check can be simplified
dotnet_diagnostic.IDE0270.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide1006
# IDE1006: Naming rule violation: These words must begin with upper case characters: ...
dotnet_diagnostic.IDE1006.severity = suggestion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ConcurrentSha256HashValidator(FileHashesDictionary fileHashesDictionary)

private async Task Validate(InternalSbomFileInfo internalFileInfo, Channel<FileValidationResult> output, Channel<FileValidationResult> errors)
{
var sha256Checksum = internalFileInfo.Checksum.Where(c => c.Algorithm == AlgorithmName.SHA256).FirstOrDefault();
var sha256Checksum = internalFileInfo.Checksum.FirstOrDefault(c => c.Algorithm == AlgorithmName.SHA256);
var fileHashes = new FileHashes();
fileHashes.SetHash(internalFileInfo.FileLocation, sha256Checksum);
FileValidationResult failureResult = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ public string GetSBOMNamespaceUri()
IMetadataProvider provider = null;
if (MetadataDictionary.TryGetValue(MetadataKey.BuildEnvironmentName, out object buildEnvironmentName))
{
provider = metadataProviders
.Where(p => p.BuildEnvironmentName != null && p.BuildEnvironmentName == buildEnvironmentName as string)
.FirstOrDefault();
provider = this.metadataProviders
.FirstOrDefault(p => p.BuildEnvironmentName != null && p.BuildEnvironmentName == buildEnvironmentName as string);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public string[] Build()

if (keyArgs.Any())
{
var keyArgsCommand = string.Join(" ", keyArgs.Select(x => AsArgumentValue(x)));
var keyArgsCommand = string.Join(" ", keyArgs.Select(this.AsArgumentValue));
command += $" {keyArgsCommand}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public async Task<IList<FileValidationResult>> GenerateAsync()
{
IList<FileValidationResult> totalErrors = new List<FileValidationResult>();

ISourcesProvider sourcesProvider = sourcesProviders
.Where(s => s.IsSupported(ProviderType.Packages))
.FirstOrDefault();
var sourcesProvider = this.sourcesProviders
.FirstOrDefault(s => s.IsSupported(ProviderType.Packages));

// Write the start of the array, if supported.
IList<ISbomConfig> packagesArraySupportingConfigs = new List<ISbomConfig>();
Expand Down
4 changes: 1 addition & 3 deletions src/Microsoft.Sbom.Contracts/Contracts/SBOMSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public static SbomSpecification Parse(string value)
}

var values = value.Split(':');
if (values == null
|| values.Length != 2
|| values.Any(v => string.IsNullOrWhiteSpace(v)))
if (values is not { Length: 2 } || values.Any(string.IsNullOrWhiteSpace))
{
throw new ArgumentException($"The SBOM specification string is not formatted correctly. The correct format is <name>:<version>.");
}
Expand Down
11 changes: 4 additions & 7 deletions src/Microsoft.Sbom.Parsers.Spdx22SbomParser/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private SPDXFile ConvertSbomFileToSpdxFile(InternalSbomFileInfo fileInfo)
FileCopyrightText = fileInfo.FileCopyrightText ?? Constants.NoAssertionValue,
LicenseConcluded = fileInfo.LicenseConcluded ?? Constants.NoAssertionValue,
LicenseInfoInFiles = fileInfo.LicenseInfoInFiles ?? Constants.NoAssertionListValue,
FileTypes = fileInfo.FileTypes?.Select(f => GetSPDXFileType(f)).ToList(),
FileTypes = fileInfo.FileTypes?.Select(this.GetSPDXFileType).ToList(),
};

spdxFileElement.AddSpdxId(fileInfo.Path, fileInfo.Checksum);
Expand Down Expand Up @@ -297,12 +297,9 @@ public GenerationResult GenerateJsonDocument(ExternalDocumentReferenceInfo exter
throw new ArgumentNullException(nameof(externalDocumentReferenceInfo.Checksum));
}

var sha1Hash = externalDocumentReferenceInfo.Checksum.Where(h => h.Algorithm == AlgorithmName.SHA1).FirstOrDefault();

if (sha1Hash is null)
{
throw new MissingHashValueException($"The hash value for algorithm {AlgorithmName.SHA1} is missing from {nameof(externalDocumentReferenceInfo)}");
}
var sha1Hash = externalDocumentReferenceInfo.Checksum.FirstOrDefault(h => h.Algorithm == AlgorithmName.SHA1) ??
throw new MissingHashValueException(
$"The hash value for algorithm {AlgorithmName.SHA1} is missing from {nameof(externalDocumentReferenceInfo)}");

var checksumValue = sha1Hash.ChecksumValue.ToLower();
var externalDocumentReferenceElement = new SpdxExternalDocumentReference
Expand Down

0 comments on commit 4c9ec94

Please sign in to comment.