@SuppressWarnings("unchecked")
    public T popState() {
        if (states.size() == 1) {
            throw new IllegalStateException("Cannot pop writer state because at the root state");
        }

        State popped = states.pop();
        currentState = states.getFirst();
        CodeSection sectionValue = popped.sectionValue;

        if (sectionValue != null) {
            // Get the contents of the current state as a string so it can be filtered.
            String result = popped.toString();

            // Don't attempt to intercept anonymous sections.
            if (!(sectionValue instanceof AnonymousCodeSection)) {
                // Ensure the remaining parent interceptors are applied in the order they were inserted.
                // This is the reverse order used when normally iterating over the states deque.
                Iterator<State> insertionOrderedStates = states.descendingIterator();
                while (insertionOrderedStates.hasNext()) {
                    State state = insertionOrderedStates.next();
                    result = applyPoppedInterceptors(popped, state, sectionValue, result);
                }
                // Now ensure the popped state's interceptors are applied.
                result = applyPoppedInterceptors(popped, popped, sectionValue, result);
            }

            if (popped.isInline) {
                // Inline sections need to be written back to the popped state, not the parent state.
                // They also can't use write because other changes to the builder since capturing
                // the result string will alter the result.
                StringBuilder builder = popped.getBuilder();
                builder.setLength(0);
                builder.append(result);
            } else if (!result.isEmpty()) {
                writeInlineWithNoFormatting(result);
            }
        }

        if (!popped.isInline && popped.needsIndentation) {
            currentState.needsIndentation = true;
        }

        return (T) this;
    }
Pops the current AbstractCodeWriter state from the state stack.

<p>This method is used to reverse a previous {@link #pushState}
operation. It configures the current AbstractCodeWriter state to what it was
before the last preceding {@code pushState} call.

@return Returns self.
@throws IllegalStateException if there a no states to pop.
smithy-utils/src/main/java/software/amazon/smithy/utils/AbstractCodeWriter.java