refactor: reduce complexity of copilot_args in engine.rs#329
Merged
jamesadevine merged 1 commit intoApr 28, 2026
Merged
Conversation
Extract two helpers to flatten deeply nested control flow in copilot_args: - collect_allowed_tools(front_matter, extensions, edit_enabled) -> Result<Vec<String>> Moves the large 'if !use_allow_all_tools' block (extension tools, MCP server tools, edit tool, bash command collection + validation) into its own function. - validate_user_arg(arg) -> Result<()> Moves the per-argument character and blocked-prefix checks out of the inner nested loop, turning it into a single flat call site. cognitive complexity: 27 → below 25 (no longer flagged by Clippy) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jamesadevine
approved these changes
Apr 28, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was complex
copilot_argsinsrc/engine.rshad a Clippy cognitive complexity of 27/25. The two main contributors were:A large
if !use_allow_all_toolsblock (≈85 lines) containing nestedforloops over extensions and MCP servers, a nestedmatchoverMcpConfig, anotherforloop for bash command collection, and yet another loop with per-command validation — all at 2–3 levels of nesting.An args validation loop with a nested
foroverBLOCKED_ARG_PREFIXES, adding an extra nesting level inside an already-nestedfor arg in ...loop.What changed
Two private helper functions were extracted:
collect_allowed_tools(front_matter, extensions, edit_enabled) -> Result<Vec<String>>Moves the entire body of the
if !use_allow_all_toolsblock into a dedicated function. Collects:writetool wheneditis enabledshell(<cmd>)tool identifiers for each allowed bash command (with single-quote validation)The call site in
copilot_argsbecomes a clean ternary:validate_user_arg(arg: &str) -> Result<()>Moves the two-level validation (character allowlist + blocked-prefix scan) for each
engine.argsentry into its own function, leaving the call site as a single flat line:Before / After
copilot_argslinescollect_allowed_tools,validate_user_arg)Verification
cargo test— all tests pass ✅cargo clippy --all-targets --all-features— no warnings ✅cargo clippy ... -W clippy::cognitive_complexity—engine.rsno longer flagged ✅Behaviour is identical; no public API signatures were changed.
Warning
The following domain was blocked by the firewall during workflow execution:
dev.azure.comTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.