Repository: servo/servo
File: components/layout/flow/inline/mod.rs (Rust)

Code before:
                    segment.runs.iter().all(|run| {
                        run.glyph_store.is_whitespace() &&
                            !run.is_single_preserved_newline() &&
                            !matches!(
                                parent_style.get_inherited_text().white_space_collapse,
                                WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
                            )
                    })
                })
            },
            InlineItem::OutOfFlowAbsolutelyPositionedBox(..) => true,
            InlineItem::OutOfFlowFloatBox(..) => true,
            InlineItem::Atomic(..) => false,
            InlineItem::AnonymousBlock(block_box) => block_box
                .borrow()
                .contents
                .find_block_margin_collapsing_with_parent(
                    layout_context,
                    collected_margin,
                    containing_block_for_children,
                ),
        })
    }

    pub(crate) fn attached_to_tree(&self, box_: WeakLayoutBox) {
        let mut parent_box_stack = vec![box_];
        for inline_item in &self.inline_items {
            match inline_item {
                InlineItem::StartInlineBox(inline_box) => {
                    inline_box
                        .borrow_mut()
                        .base
                        .parent_box
                        .replace(parent_box_stack.last().unwrap().clone());
                    parent_box_stack.push(WeakLayoutBox::InlineLevel(
                        WeakInlineItem::StartInlineBox(inline_box.downgrade()),
                    ));
                },
                InlineItem::EndInlineBox => {
                    parent_box_stack.pop();
                },
                InlineItem::TextRun(text_run) => {
                    text_run
                        .borrow_mut()
                        .parent_box
                        .replace(parent_box_stack.last().unwrap().clone().clone());
                },
                _ => inline_item.with_base_mut(|base| {
                    base.parent_box
                        .replace(parent_box_stack.last().unwrap().clone());
                }),

Review comment (suggestion):
Suggestion to avoid the unwrap (for #40744).

```suggestion
        let mut parent_box_stack = Vec::new();
        let current_layout_box = || {
            parent_box_stack.last().clone().unwrap_or(layout_box.clone())
        };
```

Code after:
                let parent_style = text_run.inline_styles.style.borrow();
                text_run.shaped_text.iter().all(|segment| {
                    segment.runs.iter().all(|run| {
                        run.glyph_store.is_whitespace() &&
                            !run.is_single_preserved_newline() &&
                            !matches!(
                                parent_style.get_inherited_text().white_space_collapse,
                                WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
                            )
                    })
                })
            },
            InlineItem::OutOfFlowAbsolutelyPositionedBox(..) => true,
            InlineItem::OutOfFlowFloatBox(..) => true,
            InlineItem::Atomic(..) => false,
            InlineItem::AnonymousBlock(block_box) => block_box
                .borrow()
                .contents
                .find_block_margin_collapsing_with_parent(
                    layout_context,
                    collected_margin,
                    containing_block_for_children,
                ),
        })
    }

    pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
        let mut parent_box_stack = Vec::new();
        let current_parent_box = |parent_box_stack: &[WeakLayoutBox]| {
            parent_box_stack.last().unwrap_or(&layout_box).clone()
        };
        for inline_item in &self.inline_items {
            match inline_item {
                InlineItem::StartInlineBox(inline_box) => {
                    inline_box
                        .borrow_mut()
                        .base
                        .parent_box
                        .replace(current_parent_box(&parent_box_stack));
                    parent_box_stack.push(WeakLayoutBox::InlineLevel(
                        WeakInlineItem::StartInlineBox(inline_box.downgrade()),
                    ));
                },
                InlineItem::EndInlineBox => {
                    parent_box_stack.pop();
                },
                InlineItem::TextRun(text_run) => {
                    text_run
                        .borrow_mut()
                        .parent_box
                        .replace(current_parent_box(&parent_box_stack));