-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1124 from AArnott/fix971
Fix VSTHRD110 performance
- Loading branch information
Showing
44 changed files
with
1,046 additions
and
1,012 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...sualStudio.Threading.Analyzers.CSharp/CSharpVSTHRD110ObserveResultOfAsyncCallsAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.VisualStudio.Threading.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class CSharpVSTHRD110ObserveResultOfAsyncCallsAnalyzer : AbstractVSTHRD110ObserveResultOfAsyncCallsAnalyzer | ||
{ | ||
private protected override LanguageUtils LanguageUtils => CSharpUtils.Instance; | ||
} |
63 changes: 0 additions & 63 deletions
63
...oft.VisualStudio.Threading.Analyzers.CSharp/VSTHRD110ObserveResultOfAsyncCallsAnalyzer.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
....Threading.Analyzers.VisualBasic/VisualBasicVSTHRD110ObserveResultOfAsyncCallsAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.VisualStudio.Threading.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.VisualBasic)] | ||
public sealed class VisualBasicVSTHRD110ObserveResultOfAsyncCallsAnalyzer : AbstractVSTHRD110ObserveResultOfAsyncCallsAnalyzer | ||
{ | ||
private protected override LanguageUtils LanguageUtils => VisualBasicUtils.Instance; | ||
} |
70 changes: 70 additions & 0 deletions
70
...ft.VisualStudio.Threading.Analyzers/AbstractVSTHRD110ObserveResultOfAsyncCallsAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Microsoft.VisualStudio.Threading.Analyzers; | ||
|
||
/// <summary> | ||
/// Report errors when async methods calls are not awaited or the result used in some way within a synchronous method. | ||
/// </summary> | ||
public abstract class AbstractVSTHRD110ObserveResultOfAsyncCallsAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string Id = "VSTHRD110"; | ||
|
||
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor( | ||
id: Id, | ||
title: new LocalizableResourceString(nameof(Strings.VSTHRD110_Title), Strings.ResourceManager, typeof(Strings)), | ||
messageFormat: new LocalizableResourceString(nameof(Strings.VSTHRD110_MessageFormat), Strings.ResourceManager, typeof(Strings)), | ||
helpLinkUri: Utils.GetHelpLink(Id), | ||
category: "Usage", | ||
defaultSeverity: DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true); | ||
|
||
/// <inheritdoc /> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Descriptor); | ||
|
||
private protected abstract LanguageUtils LanguageUtils { get; } | ||
|
||
/// <inheritdoc /> | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
CommonInterest.AwaitableTypeTester awaitableTypes = CommonInterest.CollectAwaitableTypes(context.Compilation, context.CancellationToken); | ||
context.RegisterOperationAction(Utils.DebuggableWrapper(context => this.AnalyzeInvocation(context, awaitableTypes)), OperationKind.Invocation); | ||
}); | ||
} | ||
|
||
private void AnalyzeInvocation(OperationAnalysisContext context, CommonInterest.AwaitableTypeTester awaitableTypes) | ||
{ | ||
var operation = (IInvocationOperation)context.Operation; | ||
if (operation.Type is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (operation.GetContainingFunction() is { } function && this.LanguageUtils.IsAsyncMethod(function.Syntax)) | ||
{ | ||
// CS4014 should already take care of this case. | ||
return; | ||
} | ||
|
||
// Only consider invocations that are direct statements. Otherwise, we assume their | ||
// result is awaited, assigned, or otherwise consumed. | ||
if (operation.Parent is IExpressionStatementOperation || operation.Parent is IConditionalAccessOperation) | ||
{ | ||
if (awaitableTypes.IsAwaitableType(operation.Type)) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create(Descriptor, operation.Syntax.GetLocation())); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.