From e919382ac7df7ece155187bc090377fd26230b23 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 26 Oct 2026 09:12:36 -0400 Subject: [PATCH] Extract the SCC pop step from components() to reduce cognitive complexity (S3776) Generated with Codebuff Co-Authored-By: Codebuff --- crates/calc/src/recalc.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index c34299dc6..17a0e6649 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -927,6 +927,18 @@ impl Host for PassHost<'_> { } } +/// Pops one finished component off Tarjan's stack, labelling its nodes with `comp_index`. The +/// component is everything up to and including `root`. +fn pop_component(stack: &mut Vec, on_stack: &mut [bool], comp: &mut [usize], comp_index: usize, root: usize) { + while let Some(w) = stack.pop() { + on_stack[w] = false; + comp[w] = comp_index; + if w == root { + break; + } + } +} + /// Strongly connected components of a graph given as adjacency lists (Tarjan, iterative): each /// node's component index, and the number of nodes in each component. fn components(edges: &[Vec]) -> (Vec, Vec) { @@ -965,13 +977,7 @@ fn components(edges: &[Vec]) -> (Vec, Vec) { low[u] = low[u].min(low[v]); } if low[v] == num[v] { - while let Some(w) = stack.pop() { - on_stack[w] = false; - comp[w] = comps; - if w == v { - break; - } - } + pop_component(&mut stack, &mut on_stack, &mut comp, comps, v); comps += 1; } }