rust.halstead_complexity
Maintainability
Low
Analyzes Halstead complexity metrics to identify hard-to-maintain functions.
Why It Matters
Section titled “Why It Matters”Complex code has real costs:
- Bug density — Complexity correlates with defects
- Onboarding time — New developers struggle with complex code
- Review burden — Reviewers miss issues in complex functions
- Refactoring risk — Complex code is dangerous to change
Halstead metrics quantify what “complex” means objectively.
Example
Section titled “Example”// ❌ Before (high complexity)fn process_order( order: &mut Order, user: &User, inventory: &mut HashMap<String, u32>, payments: &PaymentService,) -> Result<(), Error> { if order.status != Status::Pending { return Err(Error::InvalidStatus); } if !user.verified { return Err(Error::UserNotVerified); } if user.balance < order.total { return Err(Error::InsufficientBalance); } for item in &order.items { match inventory.get_mut(&item.id) { Some(stock) if *stock >= item.qty => { *stock -= item.qty; if *stock < 10 { notify_restock(&item.id); } } _ => return Err(Error::OutOfStock(item.id.clone())), } } payments.charge(user.id, order.total)?; order.status = Status::Complete; Ok(())}// ✅ After (decomposed)fn process_order(ctx: &mut OrderContext) -> Result<(), Error> { validate_order(&ctx.order, &ctx.user)?; let reservations = reserve_inventory(&ctx.order, &mut ctx.inventory)?;
if let Err(e) = ctx.payments.charge(ctx.user.id, ctx.order.total) { release_inventory(reservations, &mut ctx.inventory); return Err(e.into()); }
ctx.order.status = Status::Complete; Ok(())}
fn validate_order(order: &Order, user: &User) -> Result<(), Error> { if order.status != Status::Pending { return Err(Error::InvalidStatus); } if !user.verified { return Err(Error::UserNotVerified); } if user.balance < order.total { return Err(Error::InsufficientBalance); } Ok(())}What Unfault Detects
Section titled “What Unfault Detects”- Functions with difficulty above threshold
- High cyclomatic complexity
- Deeply nested control flow
- Long parameter lists
Auto-Fix
Section titled “Auto-Fix”Unfault flags complex functions, but refactoring is manual.