Skip to content

Commit 6a03a09

Browse files
Bart KoelmanBart Koelman
authored andcommitted
AV1739: Start from syntax (10% faster)
1 parent cd73017 commit 6a03a09

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Naming/UseUnderscoreForUnusedLambdaParameterAnalyzer.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using CSharpGuidelinesAnalyzer.Extensions;
55
using JetBrains.Annotations;
66
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp;
78
using Microsoft.CodeAnalysis.CSharp.Syntax;
89
using Microsoft.CodeAnalysis.Diagnostics;
9-
using Microsoft.CodeAnalysis.Operations;
1010

1111
namespace CSharpGuidelinesAnalyzer.Rules.Naming
1212
{
@@ -30,37 +30,44 @@ public sealed class UseUnderscoreForUnusedLambdaParameterAnalyzer : DiagnosticAn
3030
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
3131

3232
[NotNull]
33-
private static readonly Action<OperationAnalysisContext> AnalyzeLambdaExpressionAction =
34-
context => context.SkipInvalid(AnalyzeLambdaExpression);
33+
private static readonly SyntaxKind[] AnonymousFunctionKinds =
34+
{
35+
SyntaxKind.SimpleLambdaExpression,
36+
SyntaxKind.ParenthesizedLambdaExpression,
37+
SyntaxKind.AnonymousMethodExpression
38+
};
39+
40+
[NotNull]
41+
private static readonly Action<SyntaxNodeAnalysisContext> AnalyzeAnonymousFunctionAction = AnalyzeAnonymousFunction;
3542

3643
public override void Initialize([NotNull] AnalysisContext context)
3744
{
3845
context.EnableConcurrentExecution();
3946
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
4047

41-
context.RegisterOperationAction(AnalyzeLambdaExpressionAction, OperationKind.AnonymousFunction);
48+
context.RegisterSyntaxNodeAction(AnalyzeAnonymousFunctionAction, AnonymousFunctionKinds);
4249
}
4350

44-
private static void AnalyzeLambdaExpression(OperationAnalysisContext context)
51+
private static void AnalyzeAnonymousFunction(SyntaxNodeAnalysisContext context)
4552
{
46-
var lambdaExpression = (IAnonymousFunctionOperation)context.Operation;
53+
var anonymousFunction = (AnonymousFunctionExpressionSyntax)context.Node;
4754

48-
if (!lambdaExpression.Symbol.Parameters.Any(IsRegularParameter))
55+
if (anonymousFunction.Body == null)
4956
{
5057
return;
5158
}
5259

53-
SyntaxNode bodySyntax = lambdaExpression.Symbol.TryGetBodySyntaxForMethod(context.CancellationToken);
54-
if (bodySyntax == null)
60+
if (context.SemanticModel.GetSymbolInfo(anonymousFunction, context.CancellationToken).Symbol is IMethodSymbol method)
5561
{
56-
return;
62+
if (method.Parameters.Any(IsRegularParameter))
63+
{
64+
AnalyzeParameterUsage(method.Parameters, anonymousFunction.Body, context);
65+
}
5766
}
58-
59-
AnalyzeParameterUsage(lambdaExpression.Symbol.Parameters, bodySyntax, context);
6067
}
6168

6269
private static void AnalyzeParameterUsage([ItemNotNull] ImmutableArray<IParameterSymbol> parameters,
63-
[NotNull] SyntaxNode bodySyntax, OperationAnalysisContext context)
70+
[NotNull] SyntaxNode bodySyntax, SyntaxNodeAnalysisContext context)
6471
{
6572
DataFlowAnalysis dataFlowAnalysis = TryAnalyzeDataFlow(bodySyntax, context.Compilation);
6673
if (dataFlowAnalysis == null)
@@ -72,9 +79,7 @@ private static void AnalyzeParameterUsage([ItemNotNull] ImmutableArray<IParamete
7279
{
7380
if (IsRegularParameter(parameter) && !IsParameterUsed(parameter, dataFlowAnalysis))
7481
{
75-
string functionKind = context.Operation.Syntax is AnonymousMethodExpressionSyntax
76-
? "anonymous method"
77-
: "lambda";
82+
string functionKind = context.Node is AnonymousMethodExpressionSyntax ? "anonymous method" : "lambda";
7883
context.ReportDiagnostic(Diagnostic.Create(Rule, parameter.Locations[0], functionKind, parameter.Name));
7984
}
8085
}
@@ -111,4 +116,4 @@ private static bool IsParameterUsed([NotNull] IParameterSymbol parameter, [NotNu
111116
dataFlowAnalysis.Captured.Contains(parameter);
112117
}
113118
}
114-
}
119+
}

0 commit comments

Comments
 (0)