public static Collection <NodeSymbolInfo> GetNodesAndRelease(UnusedMemberWalker walker)
        {
            Collection <NodeSymbolInfo> nodes = walker.Nodes;

            Release(walker);

            return(nodes);
        }
        public static UnusedMemberWalker Acquire(SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            UnusedMemberWalker walker = _cachedInstance;

            if (walker != null)
            {
                _cachedInstance = null;
                walker.Reset();
            }
            else
            {
                walker = new UnusedMemberWalker();
            }

            walker.SemanticModel     = semanticModel;
            walker.CancellationToken = cancellationToken;

            return(walker);
        }
        private static void AnalyzeTypeDeclaration(SyntaxNodeAnalysisContext context, TypeDeclarationSyntax typeDeclaration)
        {
            if (typeDeclaration.Modifiers.Contains(SyntaxKind.PartialKeyword))
            {
                return;
            }

            SyntaxList <MemberDeclarationSyntax> members = typeDeclaration.Members;

            UnusedMemberWalker walker = null;

            foreach (MemberDeclarationSyntax member in members)
            {
                if (member.ContainsDiagnostics)
                {
                    continue;
                }

                if (member.ContainsDirectives)
                {
                    continue;
                }

                switch (member.Kind())
                {
                case SyntaxKind.DelegateDeclaration:
                {
                    var declaration = (DelegateDeclarationSyntax)member;

                    if (IsPrivate(declaration, declaration.Modifiers))
                    {
                        if (walker == null)
                        {
                            walker = UnusedMemberWalkerCache.Acquire(context.SemanticModel, context.CancellationToken);
                        }

                        walker.AddNode(declaration.Identifier.ValueText, declaration);
                    }

                    break;
                }

                case SyntaxKind.EventDeclaration:
                {
                    var declaration = (EventDeclarationSyntax)member;

                    if (IsPrivate(declaration, declaration.Modifiers))
                    {
                        if (walker == null)
                        {
                            walker = UnusedMemberWalkerCache.Acquire(context.SemanticModel, context.CancellationToken);
                        }

                        walker.AddNode(declaration.Identifier.ValueText, declaration);
                    }

                    break;
                }

                case SyntaxKind.EventFieldDeclaration:
                {
                    var declaration = (EventFieldDeclarationSyntax)member;

                    if (IsPrivate(declaration, declaration.Modifiers))
                    {
                        if (walker == null)
                        {
                            walker = UnusedMemberWalkerCache.Acquire(context.SemanticModel, context.CancellationToken);
                        }

                        walker.AddNodes(declaration.Declaration);
                    }

                    break;
                }

                case SyntaxKind.FieldDeclaration:
                {
                    var             declaration = (FieldDeclarationSyntax)member;
                    SyntaxTokenList modifiers   = declaration.Modifiers;

                    if (IsPrivate(declaration, modifiers))
                    {
                        if (walker == null)
                        {
                            walker = UnusedMemberWalkerCache.Acquire(context.SemanticModel, context.CancellationToken, isConst: modifiers.Contains(SyntaxKind.ConstKeyword));
                        }

                        walker.AddNodes(declaration.Declaration);
                    }

                    break;
                }

                case SyntaxKind.MethodDeclaration:
                {
                    var declaration = (MethodDeclarationSyntax)member;

                    SyntaxTokenList modifiers = declaration.Modifiers;

                    if (IsPrivate(declaration, modifiers))
                    {
                        string methodName = declaration.Identifier.ValueText;

                        if (!IsMainMethod(declaration, modifiers, methodName))
                        {
                            if (walker == null)
                            {
                                walker = UnusedMemberWalkerCache.Acquire(context.SemanticModel, context.CancellationToken);
                            }

                            walker.AddNode(methodName, declaration);
                        }
                    }

                    break;
                }

                case SyntaxKind.PropertyDeclaration:
                {
                    var declaration = (PropertyDeclarationSyntax)member;

                    if (IsPrivate(declaration, declaration.Modifiers))
                    {
                        if (walker == null)
                        {
                            walker = UnusedMemberWalkerCache.Acquire(context.SemanticModel, context.CancellationToken);
                        }

                        walker.AddNode(declaration.Identifier.ValueText, declaration);
                    }

                    break;
                }
                }
            }

            if (walker == null)
            {
                return;
            }

            walker.Visit(typeDeclaration);

            foreach (NodeSymbolInfo info in UnusedMemberWalkerCache.GetNodesAndRelease(walker))
            {
                SyntaxNode node = info.Node;

                if (node is VariableDeclaratorSyntax variableDeclarator)
                {
                    var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;

                    if (variableDeclaration.Variables.Count == 1)
                    {
                        ReportDiagnostic(context, variableDeclaration.Parent, variableDeclaration.Parent.GetTitle());
                    }
                    else
                    {
                        ReportDiagnostic(context, variableDeclarator, variableDeclaration.Parent.GetTitle());
                    }
                }
                else
                {
                    ReportDiagnostic(context, node, node.GetTitle());
                }
            }
        }
 public static void Release(UnusedMemberWalker walker)
 {
     _cachedInstance = walker;
 }