Skip to content

refactor: reduce complexity of run_analyzers in src/audit/cli.rs#1203

Merged
jamesadevine merged 1 commit into
mainfrom
refactor/reduce-complexity-run-analyzers-dd142040592d5556
Jun 25, 2026
Merged

refactor: reduce complexity of run_analyzers in src/audit/cli.rs#1203
jamesadevine merged 1 commit into
mainfrom
refactor/reduce-complexity-run-analyzers-dd142040592d5556

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

What was complex

run_analyzers (and its sibling run_agent_output_analyzers) in src/audit/cli.rs contained 13 identical match ... { Ok(r) => assign, Err(e) => warn_and_record(...) } blocks spread across the two functions — 7 in run_analyzers and 6 in run_agent_output_analyzers. Each extra match arm pushed the cognitive complexity score up, landing run_analyzers at 18/15.

What changed

New helperrun_analyzer<T, F>:

fn run_analyzer<T, F>(
    audit: &mut AuditData,
    source: &str,
    error_msg: &str,
    result: Result<T>,
    apply: F,
) where
    F: FnOnce(&mut AuditData, T),
{
    match result {
        Ok(val) => apply(audit, val),
        Err(error) => warn_and_record(audit, source, format!("{error_msg}: {:#}", error)),
    }
}

The helper lives next to warn_and_record. It takes the already-awaited Result<T> and a closure for the success path, so the match lives once (in the helper) rather than 13 times across the callers.

Refactored callers — every match block in both functions is replaced with a run_analyzer(...) call. Example before/after:

// Before
match jobs::fetch_timeline(client, ctx, auth, build_id).await {
    Ok(timeline) => audit.jobs = jobs::timeline_to_jobs(&timeline),
    Err(error) => warn_and_record(audit, "audit::jobs", format!("job timeline analysis failed: {:#}", error)),
}

// After
run_analyzer(
    audit,
    "audit::jobs",
    "job timeline analysis failed",
    jobs::fetch_timeline(client, ctx, auth, build_id).await,
    |a, timeline| a.jobs = jobs::timeline_to_jobs(&timeline),
);

Before / after complexity

Function Before After
run_analyzers 18 (flagged at threshold 15) below threshold ✅
run_agent_output_analyzers ~13 (not flagged) further reduced ✅

Verification

  • cargo build — clean
  • cargo test — all tests pass
  • cargo clippy --all-targets --all-features — no new warnings
  • cargo clippy ... -W clippy::cognitive_complexity at threshold 15 — src/audit/cli.rs line 312 no longer appears

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • spsprodeus21.vssps.visualstudio.com
  • spsprodweu4.vssps.visualstudio.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "spsprodeus21.vssps.visualstudio.com"
    - "spsprodweu4.vssps.visualstudio.com"

See Network Configuration for more information.

Generated by Cyclomatic Complexity Reducer · 568.8 AIC · ⌖ 19.9 AIC · ⊞ 36.6K ·

Extract run_analyzer<T, F> helper that encapsulates the repeated
match Ok(val)/Err(e) => warn_and_record pattern used by every
analyzer call in run_analyzers and run_agent_output_analyzers.

Both functions previously had 6-7 match blocks each, pushing
run_analyzers to cognitive complexity 18/15. The helper takes the
already-awaited Result<T> and a closure for the success case,
replacing every match arm pair with a single descriptive call site.

- run_analyzers: complexity 18 → below threshold (no warning)
- run_agent_output_analyzers: refactored consistently with same helper

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jamesadevine jamesadevine marked this pull request as ready for review June 25, 2026 10:16
@jamesadevine jamesadevine merged commit 0b39227 into main Jun 25, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant