private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context) { var methodDeclaration = (MethodDeclarationSyntax)context.Node; if (methodDeclaration.ContainsDiagnostics) { return; } if (!methodDeclaration.IsParentKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.RecordDeclaration, SyntaxKind.RecordStructDeclaration)) { return; } if (methodDeclaration.Modifiers.ContainsAny( SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword, SyntaxKind.PartialKeyword)) { return; } ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(methodDeclaration); if (!parameterInfo.Success) { return; } if (ContainsOnlyThrowNewExpression(parameterInfo.Body)) { return; } IMethodSymbol methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken); if (methodSymbol == null) { return; } if (SymbolUtility.IsEventHandlerMethod(methodSymbol)) { return; } if (!methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) { return; } if (methodSymbol.ImplementsInterfaceMember(allInterfaces: true)) { return; } UnusedParameterWalker walker = null; try { walker = UnusedParameterWalker.GetInstance(); walker.SetValues(context.SemanticModel, context.CancellationToken); FindUnusedNodes(parameterInfo, walker); if (walker.Nodes.Count > 0 && !MethodReferencedAsMethodGroupWalker.IsReferencedAsMethodGroup(methodDeclaration, methodSymbol, context.SemanticModel, context.CancellationToken)) { foreach (KeyValuePair <string, NodeSymbolInfo> kvp in walker.Nodes) { ReportDiagnostic(context, kvp.Value.Node); } } } finally { if (walker != null) { UnusedParameterWalker.Free(walker); } } }
public static void Free(UnusedParameterWalker walker) { walker.SetValues(default(SemanticModel), default(CancellationToken)); _cachedInstance = walker; }