-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathDisposeResourceAsynchronouslyAnalyzer.cs
More file actions
145 lines (116 loc) · 5.14 KB
/
DisposeResourceAsynchronouslyAnalyzer.cs
File metadata and controls
145 lines (116 loc) · 5.14 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp.Syntax;
namespace Roslynator.CSharp.Analysis;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class DisposeResourceAsynchronouslyAnalyzer : BaseDiagnosticAnalyzer
{
private static ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics;
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
if (_supportedDiagnostics.IsDefault)
Immutable.InterlockedInitialize(ref _supportedDiagnostics, DiagnosticRules.DisposeResourceAsynchronously);
return _supportedDiagnostics;
}
}
public override void Initialize(AnalysisContext context)
{
base.Initialize(context);
context.RegisterSyntaxNodeAction(f => AnalyzeLocalDeclarationStatement(f), SyntaxKind.LocalDeclarationStatement);
context.RegisterSyntaxNodeAction(f => AnalyzeUsingStatement(f), SyntaxKind.UsingStatement);
}
private static void AnalyzeLocalDeclarationStatement(SyntaxNodeAnalysisContext context)
{
var statement = (LocalDeclarationStatementSyntax)context.Node;
SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo(statement);
if (!localInfo.Success)
return;
if (!localInfo.UsingKeyword.IsKind(SyntaxKind.UsingKeyword))
return;
if (localInfo.AwaitKeyword.IsKind(SyntaxKind.AwaitKeyword))
return;
ExpressionSyntax value = localInfo.Value;
if (value is null)
return;
Analyze(context, statement, statement.UsingKeyword, value);
}
private static void AnalyzeUsingStatement(SyntaxNodeAnalysisContext context)
{
var statement = (UsingStatementSyntax)context.Node;
if (statement.AwaitKeyword.IsKind(SyntaxKind.AwaitKeyword))
return;
VariableDeclaratorSyntax declaration = statement.Declaration?.Variables.SingleOrDefault(shouldThrow: false);
if (declaration is null)
return;
ExpressionSyntax value = declaration.Initializer?.Value;
if (value is null)
return;
Analyze(context, statement, statement.UsingKeyword, value);
}
private static void Analyze(SyntaxNodeAnalysisContext context, StatementSyntax statement, SyntaxToken usingKeyword, ExpressionSyntax value)
{
ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(value, context.CancellationToken);
if (typeSymbol?.Implements(MetadataNames.System_IAsyncDisposable, allInterfaces: true) != true)
return;
for (SyntaxNode node = statement.Parent; node is not null; node = node.Parent)
{
if (node is MemberDeclarationSyntax)
{
if (node is MethodDeclarationSyntax methodDeclaration)
Analyze(context, methodDeclaration.Modifiers, usingKeyword, methodDeclaration);
break;
}
else if (node is LocalFunctionStatementSyntax localFunction)
{
Analyze(context, localFunction.Modifiers, usingKeyword, localFunction);
break;
}
else if (node is LambdaExpressionSyntax lambdaExpression)
{
Analyze(context, lambdaExpression.Modifiers, usingKeyword, lambdaExpression);
break;
}
else if (node is AnonymousMethodExpressionSyntax anonymousMethod)
{
Analyze(context, anonymousMethod.Modifiers, usingKeyword, anonymousMethod);
break;
}
else if (node is LockStatementSyntax)
{
return;
}
}
}
private static void Analyze(
SyntaxNodeAnalysisContext context,
SyntaxTokenList modifiers,
SyntaxToken usingKeyword,
SyntaxNode containingMethod)
{
if (modifiers.Contains(SyntaxKind.AsyncKeyword))
{
ReportDiagnostic(context, usingKeyword);
}
else
{
var methodSymbol = ((containingMethod.IsKind(SyntaxKind.MethodDeclaration, SyntaxKind.LocalFunctionStatement))
? context.SemanticModel.GetDeclaredSymbol(containingMethod, context.CancellationToken)
: context.SemanticModel.GetSymbol(containingMethod, context.CancellationToken)) as IMethodSymbol;
if (methodSymbol?.IsErrorType() == false
&& methodSymbol.ReturnType.IsAwaitableTaskType(context.SemanticModel, context.Node.SpanStart))
{
ReportDiagnostic(context, usingKeyword);
}
}
}
private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, SyntaxToken usingKeyword)
{
context.ReportDiagnostic(DiagnosticRules.DisposeResourceAsynchronously, usingKeyword);
}
}