Skip to content

Commit 2a128f6

Browse files
fix analyzer warnings: SA1210, SA1414, VSTHRD002, CA1849, VSTHRD103, VSTHRD104 (#341)
1 parent 06d906a commit 2a128f6

21 files changed

Lines changed: 236 additions & 252 deletions

File tree

.editorconfig

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,6 @@ dotnet_naming_rule.parameters_rule.severity = warning
463463
# Using directive should appear within a namespace declaration
464464
dotnet_diagnostic.SA1200.severity = suggestion
465465

466-
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1210.md
467-
# Using directives should be ordered alphabetically by the namespaces
468-
dotnet_diagnostic.SA1210.severity = suggestion
469-
470-
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1414.md
471-
# Tuple types in signatures should have element names
472-
dotnet_diagnostic.SA1414.severity = suggestion
473-
474466
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1600.md
475467
# Elements should be documented
476468
dotnet_diagnostic.SA1600.severity = suggestion
@@ -500,22 +492,10 @@ dotnet_diagnostic.SA1642.severity = suggestion
500492
# https://github.com/Microsoft/vs-threading
501493
##########################################
502494

503-
# https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD002.md
504-
# Synchronously waiting on tasks or awaiters may cause deadlocks. Use await or JoinableTaskFactory.Run instead.
505-
dotnet_diagnostic.VSTHRD002.severity = suggestion
506-
507-
# https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD103.md
508-
# Result synchronously blocks. Use await instead.
509-
dotnet_diagnostic.VSTHRD103.severity = suggestion
510-
511495
# https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD101.md
512496
# Avoid using async lambda for a void returning delegate type, because any exceptions not handled by the delegate will crash the process
513497
dotnet_diagnostic.VSTHRD101.severity = suggestion
514498

515-
# https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD104.md
516-
# Expose an async version of this method that does not synchronously block. Then simplify this method to call that async method within a JoinableTaskFactory.Run delegate.
517-
dotnet_diagnostic.VSTHRD104.severity = suggestion
518-
519499
# https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD111.md
520500
# Add .ConfigureAwait(bool) to your await expression
521501
dotnet_diagnostic.VSTHRD111.severity = suggestion
@@ -660,9 +640,6 @@ dotnet_diagnostic.CA1056.severity = suggestion
660640
# CA1838: Avoid 'StringBuilder' parameters for P/Invokes
661641
dotnet_diagnostic.CA1838.severity = suggestion
662642

663-
# CA1849: Call async methods when in an async method
664-
dotnet_diagnostic.CA1849.severity = suggestion
665-
666643
# CA2000: Dispose objects before losing scope
667644
dotnet_diagnostic.CA2000.severity = suggestion
668645

src/Microsoft.ComponentDetection.Common/AsyncExecution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute,
2121
throw new TimeoutException($"The execution did not complete in the alotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
2222
}
2323

24-
return work.Result;
24+
return await work;
2525
}
2626

2727
public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan timeout, CancellationToken cancellationToken)

src/Microsoft.ComponentDetection.Common/DockerReference/DockerReferenceUtility.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ public static DockerReference ParseQualifiedName(string qualifiedName)
8787
return CreateDockerReference(reference);
8888
}
8989

90-
public static (string, string) SplitDockerDomain(string name)
90+
public static (string Domain, string Remainder) SplitDockerDomain(string name)
9191
{
9292
string domain;
93-
string reminder;
93+
string remainder;
9494

9595
var indexOfSlash = name.IndexOf('/');
9696
if (indexOfSlash == -1 || !(
@@ -99,25 +99,25 @@ public static (string, string) SplitDockerDomain(string name)
9999
name.StartsWith("localhost/")))
100100
{
101101
domain = DEFAULTDOMAIN;
102-
reminder = name;
102+
remainder = name;
103103
}
104104
else
105105
{
106106
domain = name[..indexOfSlash];
107-
reminder = name[(indexOfSlash + 1)..];
107+
remainder = name[(indexOfSlash + 1)..];
108108
}
109109

110110
if (domain == LEGACYDEFAULTDOMAIN)
111111
{
112112
domain = DEFAULTDOMAIN;
113113
}
114114

115-
if (domain == DEFAULTDOMAIN && reminder.IndexOf('/') == -1)
115+
if (domain == DEFAULTDOMAIN && remainder.IndexOf('/') == -1)
116116
{
117-
reminder = $"{OFFICIALREPOSITORYNAME}/{reminder}";
117+
remainder = $"{OFFICIALREPOSITORYNAME}/{remainder}";
118118
}
119119

120-
return (domain, reminder);
120+
return (domain, remainder);
121121
}
122122

123123
public static DockerReference ParseFamiliarName(string name)

src/Microsoft.ComponentDetection.Common/Telemetry/Records/BcdeExecutionTelemetryRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static async Task<TReturn> TrackAsync<TReturn>(
4646
record.Dispose();
4747
if (terminalRecord && !(record.Command?.Equals("help", StringComparison.InvariantCultureIgnoreCase) ?? false))
4848
{
49-
TelemetryRelay.Instance.Shutdown();
49+
await TelemetryRelay.Instance.ShutdownAsync();
5050
}
5151
}
5252
}

src/Microsoft.ComponentDetection.Common/Telemetry/TelemetryRelay.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Composition;
44
using System.Linq;
55
using System.Threading;
6+
using System.Threading.Tasks;
67
using Microsoft.ComponentDetection.Common.Telemetry.Records;
78

89
namespace Microsoft.ComponentDetection.Common.Telemetry
@@ -51,7 +52,8 @@ public void PostTelemetryRecord(IDetectionTelemetryRecord record)
5152
/// <summary>
5253
/// Disables the sending of telemetry and flushes any messages out of the queue for each service.
5354
/// </summary>
54-
public void Shutdown()
55+
/// <returns><see cref="Task"/> representing the asynchronous operation.</returns>
56+
public async Task ShutdownAsync()
5557
{
5658
Active = false;
5759

@@ -60,10 +62,10 @@ public void Shutdown()
6062
try
6163
{
6264
// Set a timeout for services that flush synchronously.
63-
AsyncExecution.ExecuteVoidWithTimeoutAsync(
65+
await AsyncExecution.ExecuteVoidWithTimeoutAsync(
6466
() => service.Flush(),
6567
TimeSpan.FromSeconds(20),
66-
CancellationToken.None).Wait();
68+
CancellationToken.None);
6769
}
6870
catch
6971
{

src/Microsoft.ComponentDetection.Detectors/npm/NpmComponentDetectorWithRoots.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private async Task SafeProcessAllPackageJTokens(IComponentStream componentStream
252252
}
253253
}
254254

255-
private void TraverseRequirementAndDependencyTree(IEnumerable<(JProperty, TypedComponent)> topLevelDependencies, IDictionary<string, JProperty> dependencyLookup, ISingleFileComponentRecorder singleFileComponentRecorder)
255+
private void TraverseRequirementAndDependencyTree(IEnumerable<(JProperty Dependency, TypedComponent ParentComponent)> topLevelDependencies, IDictionary<string, JProperty> dependencyLookup, ISingleFileComponentRecorder singleFileComponentRecorder)
256256
{
257257
// iterate through everything for a top level dependency with a single explicitReferencedDependency value
258258
foreach (var (currentDependency, _) in topLevelDependencies)
@@ -292,7 +292,7 @@ private void TraverseRequirementAndDependencyTree(IEnumerable<(JProperty, TypedC
292292
}
293293
}
294294

295-
private void EnqueueDependencies(Queue<(JProperty, TypedComponent)> queue, JToken dependencies, TypedComponent parentComponent)
295+
private void EnqueueDependencies(Queue<(JProperty Dependency, TypedComponent ParentComponent)> queue, JToken dependencies, TypedComponent parentComponent)
296296
{
297297
if (dependencies != null)
298298
{
@@ -306,7 +306,7 @@ private void EnqueueDependencies(Queue<(JProperty, TypedComponent)> queue, JToke
306306
}
307307
}
308308

309-
private bool TryEnqueueFirstLevelDependencies(Queue<(JProperty, TypedComponent)> queue, JToken dependencies, IDictionary<string, JProperty> dependencyLookup, Queue<TypedComponent> parentComponentQueue = null, TypedComponent parentComponent = null, bool skipValidation = false)
309+
private bool TryEnqueueFirstLevelDependencies(Queue<(JProperty DependencyProperty, TypedComponent ParentComponent)> queue, JToken dependencies, IDictionary<string, JProperty> dependencyLookup, Queue<TypedComponent> parentComponentQueue = null, TypedComponent parentComponent = null, bool skipValidation = false)
310310
{
311311
var isValid = true;
312312
if (dependencies != null)

src/Microsoft.ComponentDetection.Detectors/pip/IPythonCommandService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public interface IPythonCommandService
88
{
99
Task<bool> PythonExists(string pythonPath = null);
1010

11-
Task<IList<(string, GitComponent)>> ParseFile(string path, string pythonPath = null);
11+
Task<IList<(string PackageString, GitComponent Component)>> ParseFile(string path, string pythonPath = null);
1212
}
1313
}

src/Microsoft.ComponentDetection.Detectors/pip/PipComponentDetector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ protected override async Task OnFileFound(ProcessRequest processRequest, IDictio
5151
try
5252
{
5353
var initialPackages = await this.PythonCommandService.ParseFile(file.Location, pythonExePath);
54-
var listedPackage = initialPackages.Where(tuple => tuple.Item1 != null)
55-
.Select(tuple => tuple.Item1)
54+
var listedPackage = initialPackages.Where(tuple => tuple.PackageString != null)
55+
.Select(tuple => tuple.PackageString)
5656
.Where(x => !string.IsNullOrWhiteSpace(x))
5757
.Select(x => new PipDependencySpecification(x))
5858
.Where(x => !x.PackageIsUnsafe())
@@ -64,8 +64,8 @@ protected override async Task OnFileFound(ProcessRequest processRequest, IDictio
6464
singleFileComponentRecorder,
6565
roots);
6666

67-
initialPackages.Where(tuple => tuple.Item2 != null)
68-
.Select(tuple => new DetectedComponent(tuple.Item2))
67+
initialPackages.Where(tuple => tuple.Component != null)
68+
.Select(tuple => new DetectedComponent(tuple.Component))
6969
.ToList()
7070
.ForEach(gitComponent => singleFileComponentRecorder.RegisterUsage(gitComponent, isExplicitReferencedDependency: true));
7171
}

src/Microsoft.ComponentDetection.Detectors/pip/PythonCommandService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task<bool> PythonExists(string pythonPath = null)
2121
return !string.IsNullOrEmpty(await this.ResolvePython(pythonPath));
2222
}
2323

24-
public async Task<IList<(string, GitComponent)>> ParseFile(string filePath, string pythonPath = null)
24+
public async Task<IList<(string PackageString, GitComponent Component)>> ParseFile(string filePath, string pythonPath = null)
2525
{
2626
if (string.IsNullOrEmpty(filePath))
2727
{
@@ -69,7 +69,7 @@ private async Task<IList<string>> ParseSetupPyFile(string filePath, string pytho
6969
return result.Split(new string[] { "'," }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim().Trim('\'').Trim()).ToList();
7070
}
7171

72-
private IList<(string, GitComponent)> ParseRequirementsTextFile(string path)
72+
private IList<(string PackageString, GitComponent Component)> ParseRequirementsTextFile(string path)
7373
{
7474
var items = new List<(string, GitComponent)>();
7575
foreach (var line in File.ReadAllLines(path).Select(x => x.Trim().TrimEnd('\\')).Where(x => !x.StartsWith("#") && !x.StartsWith("-") && !string.IsNullOrWhiteSpace(x)))

src/Microsoft.ComponentDetection.Detectors/pip/PythonResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private class PythonResolverState
211211
public IDictionary<string, SortedDictionary<string, IList<PythonProjectRelease>>> ValidVersionMap { get; }
212212
= new Dictionary<string, SortedDictionary<string, IList<PythonProjectRelease>>>(StringComparer.OrdinalIgnoreCase);
213213

214-
public Queue<(string, PipDependencySpecification)> ProcessingQueue { get; } = new Queue<(string, PipDependencySpecification)>();
214+
public Queue<(string PackageName, PipDependencySpecification Package)> ProcessingQueue { get; } = new Queue<(string, PipDependencySpecification)>();
215215

216216
public IDictionary<string, PipGraphNode> NodeReferences { get; } = new Dictionary<string, PipGraphNode>(StringComparer.OrdinalIgnoreCase);
217217

0 commit comments

Comments
 (0)