-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathTaskHelper.cs
More file actions
49 lines (40 loc) · 1.9 KB
/
TaskHelper.cs
File metadata and controls
49 lines (40 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Helpers
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
internal static class TaskHelper
{
public static readonly Task CompletedTask = Task.FromResult(0);
public static bool IsTaskReturningMethod(SemanticModel semanticModel, MethodDeclarationSyntax methodDeclarationSyntax, CancellationToken cancellationToken)
{
return IsTaskType(semanticModel, methodDeclarationSyntax.ReturnType, cancellationToken);
}
public static bool IsTaskReturningMethod(SemanticModel semanticModel, DelegateDeclarationSyntax delegateDeclarationSyntax, CancellationToken cancellationToken)
{
return IsTaskType(semanticModel, delegateDeclarationSyntax.ReturnType, cancellationToken);
}
public static bool IsTaskType(SemanticModel semanticModel, TypeSyntax typeSyntax, CancellationToken cancellationToken)
{
SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(typeSyntax, cancellationToken);
if (!(symbolInfo.Symbol is INamedTypeSymbol namedTypeSymbol))
{
return false;
}
if (!string.Equals(nameof(Task), namedTypeSymbol.Name, StringComparison.Ordinal))
{
return false;
}
if (!string.Equals(typeof(Task).Namespace, namedTypeSymbol.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)), StringComparison.Ordinal))
{
return false;
}
return true;
}
}
}