Skip to content

Commit

Permalink
Fix some analyzer warnings (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotic authored Jan 31, 2023
1 parent cf622c7 commit 4e49e43
Show file tree
Hide file tree
Showing 265 changed files with 1,560 additions and 1,744 deletions.
83 changes: 0 additions & 83 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,6 @@ dotnet_naming_rule.parameters_rule.severity = warning
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers
##########################################

# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1200.md
# Using directive should appear within a namespace declaration
dotnet_diagnostic.SA1200.severity = suggestion

# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1600.md
# Elements should be documented
dotnet_diagnostic.SA1600.severity = suggestion
Expand Down Expand Up @@ -500,10 +496,6 @@ dotnet_diagnostic.VSTHRD101.severity = suggestion
# Add .ConfigureAwait(bool) to your await expression
dotnet_diagnostic.VSTHRD111.severity = suggestion

# https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD200.md
# Use "Async" suffix in names of methods that return an awaitable type
dotnet_diagnostic.VSTHRD200.severity = suggestion

##########################################
# Provided by Roslyn
# https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/
Expand All @@ -517,9 +509,6 @@ dotnet_diagnostic.CS0618.severity = suggestion
# Missing XML comment for publicly visible type or member '...'
dotnet_diagnostic.CS1591.severity = suggestion

# IDE0003: Remove qualification
dotnet_diagnostic.IDE0003.severity = suggestion

# CA2007: Consider calling ConfigureAwait on the awaited task
dotnet_diagnostic.CA2007.severity = suggestion

Expand All @@ -538,45 +527,21 @@ dotnet_diagnostic.CA1031.severity = suggestion
# CA1711: Identifiers should not have incorrect suffix
dotnet_diagnostic.CA1711.severity = suggestion

# CA1018: Mark attributes with AttributeUsageAttribute
dotnet_diagnostic.CA1018.severity = suggestion

# CA1813: Avoid unsealed attributes
dotnet_diagnostic.CA1813.severity = suggestion

# CA2227: Collection properties should be read only
dotnet_diagnostic.CA2227.severity = suggestion

# CA1825: Avoid zero-length array allocations
dotnet_diagnostic.CA1825.severity = suggestion

# CA1310: Specify StringComparison for correctness
dotnet_diagnostic.CA1310.severity = suggestion

# CA1002: Do not expose generic lists
dotnet_diagnostic.CA1002.severity = suggestion

# IDE0008: Use explicit type
dotnet_diagnostic.IDE0008.severity = suggestion

# CA1305: Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = suggestion

# CA1032: Implement standard exception constructors
dotnet_diagnostic.CA1032.severity = suggestion

# CA1051: Do not declare visible instance fields
dotnet_diagnostic.CA1051.severity = suggestion

# CA1401: P/Invokes should not be visible
dotnet_diagnostic.CA1401.severity = suggestion

# CA5392: Use DefaultDllImportSearchPaths attribute for P/Invokes
dotnet_diagnostic.CA5392.severity = suggestion

# CA2101: Specify marshaling for P/Invoke string arguments
dotnet_diagnostic.CA2101.severity = suggestion

# CA2211: Non-constant fields should not be visible
dotnet_diagnostic.CA2211.severity = suggestion

Expand All @@ -592,42 +557,18 @@ dotnet_diagnostic.CA1822.severity = suggestion
# CA1307: Specify StringComparison for clarity
dotnet_diagnostic.CA1307.severity = suggestion

# CA1052: Static holder types should be Static or NotInheritable
dotnet_diagnostic.CA1052.severity = suggestion

# CA1725: Parameter names should match base declaration
dotnet_diagnostic.CA1725.severity = suggestion

# CA1805: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = suggestion

# CA1806: Do not ignore method results
dotnet_diagnostic.CA1806.severity = suggestion

# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = suggestion

# CA1816: Dispose methods should call SuppressFinalize
dotnet_diagnostic.CA1816.severity = suggestion

# CA2249: Consider using 'string.Contains' instead of 'string.IndexOf'
dotnet_diagnostic.CA2249.severity = suggestion

# CA1836: Prefer IsEmpty over Count
dotnet_diagnostic.CA1836.severity = suggestion

# CA2208: Instantiate argument exceptions correctly
dotnet_diagnostic.CA2208.severity = suggestion

# CA1028: Enum Storage should be Int32
dotnet_diagnostic.CA1028.severity = suggestion

# CA1034: Nested types should not be visible
dotnet_diagnostic.CA1034.severity = suggestion

# CA1033: Interface methods should be callable by child types
dotnet_diagnostic.CA1033.severity = suggestion

# CA1054: URI-like parameters should not be strings
dotnet_diagnostic.CA1054.severity = suggestion

Expand All @@ -637,42 +578,18 @@ dotnet_diagnostic.CA1027.severity = suggestion
# CA1056: URI-like properties should not be strings
dotnet_diagnostic.CA1056.severity = suggestion

# CA1838: Avoid 'StringBuilder' parameters for P/Invokes
dotnet_diagnostic.CA1838.severity = suggestion

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = suggestion

# CA2016: Forward the 'CancellationToken' parameter to methods
dotnet_diagnostic.CA2016.severity = suggestion

# CA1014: Mark assemblies with CLSCompliantAttribute
dotnet_diagnostic.CA1014.severity = none

# CA1824: Mark assemblies with NeutralResourcesLanguageAttribute
dotnet_diagnostic.CA1824.severity = none

# CA1508: Avoid dead conditional code
dotnet_diagnostic.CA1508.severity = suggestion

# CA1823: Avoid unused private fields
dotnet_diagnostic.CA1823.severity = suggestion

# CA2234: Pass system uri objects instead of strings
dotnet_diagnostic.CA2234.severity = suggestion

# SA1101: Prefix local calls with this
dotnet_diagnostic.SA1101.severity = suggestion

# CA2219: Do not raise exceptions in finally clauses
dotnet_diagnostic.CA2219.severity = suggestion

# CA2201: Exception type System.Exception is not sufficiently specific
dotnet_diagnostic.CA2201.severity = suggestion

# CA1835: Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync'
dotnet_diagnostic.CA1835.severity = suggestion

# CA1036: Override methods on comparable types
dotnet_diagnostic.CA1036.severity = suggestion

Expand Down
7 changes: 3 additions & 4 deletions src/Microsoft.ComponentDetection.Common/AsyncExecution.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.ComponentDetection.Common;

public static class AsyncExecution
{
public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute, TimeSpan timeout, CancellationToken cancellationToken)
Expand Down Expand Up @@ -31,7 +30,7 @@ public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan
throw new ArgumentNullException(nameof(toExecute));
}

var work = Task.Run(toExecute);
var work = Task.Run(toExecute, cancellationToken);
var completedInTime = await Task.Run(() => work.Wait(timeout));
if (!completedInTime)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -10,17 +11,15 @@
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;

namespace Microsoft.ComponentDetection.Common;

[Export(typeof(ICommandLineInvocationService))]
public class CommandLineInvocationService : ICommandLineInvocationService
{
private readonly IDictionary<string, string> commandLocatableCache = new ConcurrentDictionary<string, string>();

public async Task<bool> CanCommandBeLocated(string command, IEnumerable<string> additionalCandidateCommands = null, DirectoryInfo workingDirectory = null, params string[] parameters)
public async Task<bool> CanCommandBeLocatedAsync(string command, IEnumerable<string> additionalCandidateCommands = null, DirectoryInfo workingDirectory = null, params string[] parameters)
{
additionalCandidateCommands ??= Enumerable.Empty<string>();
parameters ??= new string[0];
parameters ??= Array.Empty<string>();
var allCommands = new[] { command }.Concat(additionalCandidateCommands);
if (!this.commandLocatableCache.TryGetValue(command, out var validCommand))
{
Expand Down Expand Up @@ -51,19 +50,19 @@ public async Task<bool> CanCommandBeLocated(string command, IEnumerable<string>
return !string.IsNullOrWhiteSpace(validCommand);
}

public async Task<CommandLineExecutionResult> ExecuteCommand(string command, IEnumerable<string> additionalCandidateCommands = null, DirectoryInfo workingDirectory = null, params string[] parameters)
public async Task<CommandLineExecutionResult> ExecuteCommandAsync(string command, IEnumerable<string> additionalCandidateCommands = null, DirectoryInfo workingDirectory = null, params string[] parameters)
{
var isCommandLocatable = await this.CanCommandBeLocated(command, additionalCandidateCommands);
var isCommandLocatable = await this.CanCommandBeLocatedAsync(command, additionalCandidateCommands);
if (!isCommandLocatable)
{
throw new InvalidOperationException(
$"{nameof(this.ExecuteCommand)} was called with a command that could not be located: `{command}`!");
$"{nameof(this.ExecuteCommandAsync)} was called with a command that could not be located: `{command}`!");
}

if (workingDirectory != null && !Directory.Exists(workingDirectory.FullName))
{
throw new InvalidOperationException(
$"{nameof(this.ExecuteCommand)} was called with a working directory that could not be located: `{workingDirectory.FullName}`");
$"{nameof(this.ExecuteCommandAsync)} was called with a working directory that could not be located: `{workingDirectory.FullName}`");
}

using var record = new CommandLineInvocationTelemetryRecord();
Expand All @@ -88,14 +87,14 @@ public bool IsCommandLineExecution()
return true;
}

public async Task<bool> CanCommandBeLocated(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
public async Task<bool> CanCommandBeLocatedAsync(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.CanCommandBeLocated(command, additionalCandidateCommands, workingDirectory: null, parameters);
return await this.CanCommandBeLocatedAsync(command, additionalCandidateCommands, workingDirectory: null, parameters);
}

public async Task<CommandLineExecutionResult> ExecuteCommand(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
public async Task<CommandLineExecutionResult> ExecuteCommandAsync(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.ExecuteCommand(command, additionalCandidateCommands, workingDirectory: null, parameters);
return await this.ExecuteCommandAsync(command, additionalCandidateCommands, workingDirectory: null, parameters);
}

private static Task<CommandLineExecutionResult> RunProcessAsync(string fileName, string parameters, DirectoryInfo workingDirectory = null)
Expand Down
13 changes: 6 additions & 7 deletions src/Microsoft.ComponentDetection.Common/ComponentComparer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System.Collections.Generic;
namespace Microsoft.ComponentDetection.Common;
using System.Collections.Generic;
using Microsoft.ComponentDetection.Contracts.TypedComponent;

namespace Microsoft.ComponentDetection.Common;

public class ComponentComparer : EqualityComparer<TypedComponent>
{
public override bool Equals(TypedComponent t0, TypedComponent t1)
public override bool Equals(TypedComponent x, TypedComponent y)
{
return t0.Id.Equals(t1.Id);
return x.Id.Equals(y.Id);
}

public override int GetHashCode(TypedComponent typedComponent)
public override int GetHashCode(TypedComponent obj)
{
return typedComponent.Id.GetHashCode();
return obj.Id.GetHashCode();
}
}
5 changes: 2 additions & 3 deletions src/Microsoft.ComponentDetection.Common/ComponentStream.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.IO;
namespace Microsoft.ComponentDetection.Common;
using System.IO;
using Microsoft.ComponentDetection.Contracts;

namespace Microsoft.ComponentDetection.Common;

public class ComponentStream : IComponentStream
{
public Stream Stream { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Microsoft.ComponentDetection.Contracts;

namespace Microsoft.ComponentDetection.Common;

public class ComponentStreamEnumerable : IEnumerable<IComponentStream>
{
public ComponentStreamEnumerable(IEnumerable<MatchedFile> fileEnumerable, ILogger logger)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using Microsoft.ComponentDetection.Contracts;

namespace Microsoft.ComponentDetection.Common;

[Export(typeof(IComponentStreamEnumerableFactory))]
[Shared]
public class ComponentStreamEnumerableFactory : IComponentStreamEnumerableFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Composition;

namespace Microsoft.ComponentDetection.Common;

[Export(typeof(IConsoleWritingService))]
public class ConsoleWritingService : IConsoleWritingService
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal DependencyGraph GetDependencyGraphForLocation(string location)
return this.singleFileRecorders.Single(x => x.ManifestFileLocation == location).DependencyGraph;
}

public class SingleFileComponentRecorder : ISingleFileComponentRecorder
public sealed class SingleFileComponentRecorder : ISingleFileComponentRecorder
{
private readonly ILogger log;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void AddComponent(ComponentRefNode componentNode, string parentComponentI

if (string.IsNullOrWhiteSpace(componentNode.Id))
{
throw new ArgumentNullException(nameof(componentNode.Id));
throw new ArgumentNullException(nameof(componentNode.Id), "Invalid component node id");
}

this.componentNodes.AddOrUpdate(componentNode.Id, componentNode, (key, currentNode) =>
Expand Down Expand Up @@ -99,7 +99,7 @@ public HashSet<string> GetAdditionalRelatedFiles()

public bool HasComponents()
{
return this.componentNodes.Count > 0;
return !this.componentNodes.IsEmpty;
}

public bool? IsDevelopmentDependency(string componentId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Microsoft.ComponentDetection.Contracts.BcdeModels;

namespace Microsoft.ComponentDetection.Common;
namespace Microsoft.ComponentDetection.Common;
using Microsoft.ComponentDetection.Contracts.BcdeModels;

/// <summary>
/// Merges dependnecy Scope in their order of Priority.
/// Higher priority scope, as indicated by its lower enum value is given precendence.
/// </summary>
public class DependencyScopeComparer
public static class DependencyScopeComparer
{
public static DependencyScope? GetMergedDependencyScope(DependencyScope? scope1, DependencyScope? scope2)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Composition;
namespace Microsoft.ComponentDetection.Common;
using System.Composition;
using Microsoft.ComponentDetection.Contracts;

namespace Microsoft.ComponentDetection.Common;

[Export(typeof(IDetectorDependencies))]
public class DetectorDependencies : IDetectorDependencies
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Generic;
namespace Microsoft.ComponentDetection.Common;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.ComponentDetection.Contracts;

namespace Microsoft.ComponentDetection.Common;

[DebuggerDisplay("{Name}")]
public class DirectoryItemFacade
{
Expand Down
Loading

0 comments on commit 4e49e43

Please sign in to comment.