Skip to content

Commit

Permalink
Merge pull request #1124 from AArnott/fix971
Browse files Browse the repository at this point in the history
Fix VSTHRD110 performance
  • Loading branch information
AArnott committed Dec 16, 2022
2 parents f20115a + c7ceed0 commit 1738f83
Show file tree
Hide file tree
Showing 44 changed files with 1,046 additions and 1,012 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ internal override bool MethodReturnsNullableReferenceType(IMethodSymbol methodSy
return returnType is not null && returnType.IsKind(SyntaxKind.NullableType);
}

internal override bool IsAsyncMethod(SyntaxNode syntaxNode)
{
SyntaxTokenList? modifiers = syntaxNode switch
{
MethodDeclarationSyntax methodDeclaration => methodDeclaration.Modifiers,
SimpleLambdaExpressionSyntax lambda => lambda.Modifiers,
AnonymousMethodExpressionSyntax anonMethod => anonMethod.Modifiers,
ParenthesizedLambdaExpressionSyntax lambda => lambda.Modifiers,
_ => null,
};
return modifiers?.Any(SyntaxKind.AsyncKeyword) is true;
}

internal readonly struct ContainingFunctionData
{
internal ContainingFunctionData(CSharpSyntaxNode function, bool isAsync, ParameterListSyntax? parameterList, CSharpSyntaxNode? blockOrExpression, Func<CSharpSyntaxNode, CSharpSyntaxNode> bodyReplacement)
Expand Down
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;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;

namespace Microsoft.VisualStudio.Threading.Analyzers;
Expand Down Expand Up @@ -93,4 +94,6 @@ internal override bool MethodReturnsNullableReferenceType(IMethodSymbol methodSy
// VB.NET doesn't support nullable reference types
return false;
}

internal override bool IsAsyncMethod(SyntaxNode syntaxNode) => syntaxNode is MethodBlockSyntax methodDeclaration && methodDeclaration.BlockStatement.Modifiers.Any(SyntaxKind.AsyncKeyword);
}
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;
}
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()));
}
}
}
}

0 comments on commit 1738f83

Please sign in to comment.