Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Syntax/SyntaxTreeDiagnosticEnumerator.cs (C#)

Code before:
                    stack.MarkProcessedDiagnosticsForStackTop();
                }

                if (tryContinueWithThisNode(node))
                    continue;

                // Weren't able to continue with this node. Pop it so we continue processing its parent.
                stack.Pop();
            }

            yield break;

            bool tryContinueWithThisNode(GreenNode node)
            {
                // SlotCount is 0 when we hit tokens or normal trivia. In this case, we just want to move past the
                // normal width of the item.  Importantly, in the case of tokens, we don't want to move the
                // full-width.  That would make us double-count the widths of the leading/trailing trivia which
                // we're walking into as normal green nodes.
                if (node.SlotCount == 0)
                {
                    position += node.Width;
                }
                else
                {
                    var nextSlotIndex = stack.Top.SlotIndex;
                    while (++nextSlotIndex < node.SlotCount)
                    {
                        var child = node.GetSlot(nextSlotIndex);
                        if (child == null)
                            continue;

                        // If the child doesn't have diagnostics anywhere in it, we can skip it entirely.
                        if (!child.ContainsDiagnostics)
                        {
                            position += child.FullWidth;
                            continue;
                        }

                        stack.UpdateSlotIndexForStackTop(nextSlotIndex);
                        stack.PushNodeOrToken(child);

                        return true;
                    }
                }

                // Weren't able to continue with this node. Pop it so we continue processing its parent.
                return false;
            }
        }

        private struct NodeIteration(GreenNode node)

Review comment (suggestion):
Consider using a form like the following

```suggestion
                    for (var nextSlotIndex = stack.Top.SlotIndex + 1; nextSlotIndex < node.SlotCount; nextSlotIndex++)
```

so that I don't have to think as hard.

Code after:
                    stack.Top.ProcessedDiagnostics = true;
                }

                processNode(node);
            }

            yield break;

            void processNode(GreenNode node)
            {
                // SlotCount is 0 when we hit tokens or normal trivia. In this case, we just want to move past the
                // normal width of the item.  Importantly, in the case of tokens, we don't want to move the full-width.
                // That would make us double-count the widths of the leading/trailing trivia which we're walking into as
                // normal green nodes.
                //
                // As this has no children and has had its diagnostics processed, pop it and process its parent.
                if (node.SlotCount == 0)
                {
                    position += node.Width;
                }
                else
                {
                    for (var nextSlotIndex = stack.Top.SlotIndex + 1; nextSlotIndex < node.SlotCount; nextSlotIndex++)
                    {
                        var child = node.GetSlot(nextSlotIndex);
                        if (child == null)
                            continue;

                        // If the child doesn't have diagnostics anywhere in it, we can skip it entirely.
                        if (!child.ContainsDiagnostics)
                        {
                            position += child.FullWidth;
                            continue;
                        }

                        stack.Top.SlotIndex = nextSlotIndex;
                        stack.PushNodeOrToken(child);

                        // Pushed a child to process, return out to continue processing it.
                        return;
                    }
                }

                // Done with this node. Pop it so we continue processing its parent.
                stack.Pop();
            }
        }

        private struct NodeIteration(GreenNode node)
        {
            /// <summary>