Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit 232add8

Browse files
committed
Simplify analyzers using existing helper methods
1 parent 4893308 commit 232add8

File tree

3 files changed

+12
-86
lines changed

3 files changed

+12
-86
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers/Naming/AvoidAsyncSuffixAnalyzer.cs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace AsyncUsageAnalyzers.Naming
77
using System.Collections.Concurrent;
88
using System.Collections.Immutable;
99
using System.Threading.Tasks;
10+
using AsyncUsageAnalyzers.Helpers;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.Diagnostics;
1213

@@ -60,63 +61,26 @@ public Analyzer(ConcurrentDictionary<SyntaxTree, bool> generatedHeaderCache)
6061
public void HandleMethodDeclaration(SymbolAnalysisContext context)
6162
{
6263
IMethodSymbol symbol = (IMethodSymbol)context.Symbol;
63-
if (symbol.IsAsync)
64-
{
65-
return;
66-
}
67-
6864
if (!symbol.Name.EndsWith("Async", StringComparison.Ordinal))
6965
{
7066
return;
7167
}
7268

73-
if (symbol.IsOverride)
74-
{
75-
return;
76-
}
77-
78-
if (!symbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
69+
if (symbol.HasAsyncSignature(treatAsyncVoidAsAsync: true))
7970
{
8071
return;
8172
}
8273

83-
if (symbol.Locations.IsDefaultOrEmpty)
74+
if (!symbol.IsInAnalyzedSource(this.generatedHeaderCache, context.CancellationToken))
8475
{
8576
return;
8677
}
8778

88-
Location location = symbol.Locations[0];
89-
if (!location.IsInSource || location.SourceTree.IsGeneratedDocument(this.generatedHeaderCache, context.CancellationToken))
79+
if (symbol.IsOverrideOrImplementation())
9080
{
9181
return;
9282
}
9383

94-
if (!symbol.ReturnsVoid)
95-
{
96-
// This check conveniently covers Task and Task<T> by ignoring the `1 in Task<T>.
97-
if (string.Equals(nameof(Task), symbol.ReturnType?.Name, StringComparison.Ordinal)
98-
&& string.Equals(typeof(Task).Namespace, symbol.ReturnType?.ContainingNamespace?.ToString(), StringComparison.Ordinal))
99-
{
100-
return;
101-
}
102-
}
103-
104-
if ((symbol.ContainingType.TypeKind == TypeKind.Class || symbol.ContainingType.TypeKind == TypeKind.Struct)
105-
&& symbol.DeclaredAccessibility == Accessibility.Public)
106-
{
107-
// As a final check, make sure the method isn't implicitly implementing an interface method
108-
foreach (INamedTypeSymbol interfaceType in symbol.ContainingType.AllInterfaces)
109-
{
110-
foreach (var member in interfaceType.GetMembers(symbol.Name))
111-
{
112-
if (Equals(symbol.ContainingType.FindImplementationForInterfaceMember(member), symbol))
113-
{
114-
return;
115-
}
116-
}
117-
}
118-
}
119-
12084
context.ReportDiagnostic(Diagnostic.Create(Descriptor, symbol.Locations[0], symbol.Name));
12185
}
12286
}

AsyncUsageAnalyzers/AsyncUsageAnalyzers/Naming/UseAsyncSuffixAnalyzer.cs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace AsyncUsageAnalyzers.Naming
77
using System.Collections.Concurrent;
88
using System.Collections.Immutable;
99
using System.Threading.Tasks;
10+
using AsyncUsageAnalyzers.Helpers;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.Diagnostics;
1213

@@ -65,40 +66,12 @@ public void HandleMethodDeclaration(SymbolAnalysisContext context)
6566
return;
6667
}
6768

68-
if (symbol.Locations.IsDefaultOrEmpty)
69+
if (!symbol.IsInAnalyzedSource(this.generatedHeaderCache, context.CancellationToken))
6970
{
7071
return;
7172
}
7273

73-
if (symbol.IsOverride)
74-
{
75-
return;
76-
}
77-
78-
if (!symbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
79-
{
80-
return;
81-
}
82-
83-
Location location = symbol.Locations[0];
84-
if (!location.IsInSource || location.SourceTree.IsGeneratedDocument(this.generatedHeaderCache, context.CancellationToken))
85-
{
86-
return;
87-
}
88-
89-
// void-returning methods are not asynchronous according to their signature, even if they use `async`
90-
if (symbol.ReturnsVoid)
91-
{
92-
return;
93-
}
94-
95-
// This check conveniently covers Task and Task<T> by ignoring the `1 in Task<T>.
96-
if (!string.Equals(nameof(Task), symbol.ReturnType?.Name, StringComparison.Ordinal))
97-
{
98-
return;
99-
}
100-
101-
if (!string.Equals(typeof(Task).Namespace, symbol.ReturnType?.ContainingNamespace?.ToString(), StringComparison.Ordinal))
74+
if (!symbol.HasAsyncSignature())
10275
{
10376
return;
10477
}
@@ -108,20 +81,9 @@ public void HandleMethodDeclaration(SymbolAnalysisContext context)
10881
return;
10982
}
11083

111-
if ((symbol.ContainingType.TypeKind == TypeKind.Class || symbol.ContainingType.TypeKind == TypeKind.Struct)
112-
&& symbol.DeclaredAccessibility == Accessibility.Public)
84+
if (symbol.IsOverrideOrImplementation())
11385
{
114-
// As a final check, make sure the method isn't implicitly implementing an interface method
115-
foreach (INamedTypeSymbol interfaceType in symbol.ContainingType.AllInterfaces)
116-
{
117-
foreach (var member in interfaceType.GetMembers(symbol.Name))
118-
{
119-
if (Equals(symbol.ContainingType.FindImplementationForInterfaceMember(member), symbol))
120-
{
121-
return;
122-
}
123-
}
124-
}
86+
return;
12587
}
12688

12789
context.ReportDiagnostic(Diagnostic.Create(Descriptor, symbol.Locations[0], symbol.Name));

AsyncUsageAnalyzers/AsyncUsageAnalyzers/Reliability/AvoidAsyncVoidAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace AsyncUsageAnalyzers.Reliability
66
using System;
77
using System.Collections.Concurrent;
88
using System.Collections.Immutable;
9+
using AsyncUsageAnalyzers.Helpers;
910
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.CSharp;
1112
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -104,13 +105,12 @@ public void HandleMethodDeclaration(SymbolAnalysisContext context)
104105
return;
105106
}
106107

107-
Location location = symbol.Locations[0];
108-
if (!location.IsInSource || location.SourceTree.IsGeneratedDocument(this.generatedHeaderCache, context.CancellationToken))
108+
if (!symbol.IsInAnalyzedSource(this.generatedHeaderCache, context.CancellationToken))
109109
{
110110
return;
111111
}
112112

113-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location, symbol.Name));
113+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, symbol.Locations[0], symbol.Name));
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)