rust-doctor
Rules

Architecture Rules

Rules for detecting architectural anti-patterns and complexity issues in Rust code.

high-cyclomatic-complexity

Severity: Warning

Detects functions with cyclomatic complexity exceeding 15. High complexity correlates with bug density and makes functions harder to test, understand, and maintain.

// Bad — deeply nested control flow
fn process(input: &Input) -> Result<Output> {
    if input.valid {
        if input.mode == Mode::A {
            if input.value > 0 {
                if input.flag {
                    // ... many more branches
                }
            }
        }
    }
    // ...
}

// Good — extract sub-functions, use early returns
fn process(input: &Input) -> Result<Output> {
    validate(input)?;
    match input.mode {
        Mode::A => process_mode_a(input),
        Mode::B => process_mode_b(input),
    }
}

Fix strategies:

  • Extract sub-functions for each logical branch
  • Use early returns / guard clauses to reduce nesting
  • Replace nested if/else chains with match expressions
  • Consider the strategy pattern for complex dispatch logic

On this page