rustqual
Comprehensive Rust code quality analyzer — six dimensions: Complexity, Coupling, DRY, IOSP, SRP, Test Quality — plus 7 structural binary checks integrated into SRP and Coupling. Particularly useful as a structural quality guardrail for AI-generated code, catching the god-functions, mixed concerns, duplicated patterns, and weak tests that AI coding agents commonly produce.
Quality Dimensions
rustqual analyzes your Rust code across seven quality dimensions, each contributing to an overall quality score:
| Dimension | Weight | What it checks |
|---|---|---|
| IOSP | 22% | Function separation (Integration vs Operation) |
| Complexity | 18% | Cognitive/cyclomatic complexity, magic numbers, nesting depth, function length, unsafe blocks, error handling |
| DRY | 13% | Duplicate functions, fragments, dead code, boilerplate |
| SRP | 18% | Struct cohesion (LCOM4), module length, function clusters, structural checks (BTC, SLM, NMS) |
| Coupling | 9% | Module instability, circular dependencies, SDP, structural checks (OI, SIT, DEH, IET) |
| Test Quality | 10% | Assertion density, no-SUT tests, untested functions, coverage gaps, untested logic |
| Architecture | 10% | Layer ordering, forbidden-edge rules, symbol patterns (path/method/function/macro/derive/item-kind), trait-signature contracts |
What is IOSP?
The Integration Operation Segregation Principle (from Ralf Westphal's Flow Design) states that every function should be either:
- Integration — orchestrates other functions, contains no logic of its own
- Operation — contains logic (control flow, computation), but does not call other "own" functions
A function that does both is a violation. A function too small to matter (empty body, single expression without logic or own calls) is classified as Trivial.
┌─────────────┐ ┌─────────────┐ ┌────────────────────┐
│ Integration │ │ Operation │ │ ✗ Violation │
│ │ │ │ │ │
│ calls A() │ │ if x > 0 │ │ if x > 0 │
│ calls B() │ │ y = x*2 │ │ result = calc() │ ← mixes both
│ calls C() │ │ return y │ │ return result + 1 │
└─────────────┘ └─────────────┘ └────────────────────┘
Installation
# From crates.io
# From source
# Then use either:
Quick Start
# Analyze current directory (default — matches architecture-rule globs)
# Analyze a specific file or directory
# Show all functions, not just findings
# Do not exit with code 1 on findings (for local exploration)
# Generate a default config file
# Watch mode: re-analyze on file changes
Using AI coding agents? See Using with AI Coding Agents for integration patterns with Claude Code, Cursor, Copilot, and other tools.
Output Formats
Text (default)
── src/order.rs
✓ INTEGRATION process_order (line 12)
✓ OPERATION calculate_discount (line 28)
Complexity: logic=2, calls=0, nesting=1, cognitive=2, cyclomatic=3
✗ VIOLATION process_payment (line 48) [MEDIUM]
Logic: if (line 50), comparison (line 50), if (line 56)
Calls: determine_payment_method (line 55), charge_credit_card (line 59)
Complexity: logic=3, calls=2, nesting=1, cognitive=5, cyclomatic=4
· TRIVIAL get_name (line 72)
~ SUPPRESSED legacy_handler (line 85)
═══ Summary ═══
Functions: 24 Quality Score: 82.3%
IOSP: 85.7% (4I, 8O, 10T, 2 violations)
Complexity: 90.0% (3 complexity, 1 magic numbers)
DRY: 95.0% (1 duplicates, 2 dead code)
SRP: 100.0%
Test Quality: 100.0%
Coupling: 100.0%
~ Suppressed: 1
4 quality findings. Run with --verbose for details.
JSON
# or
Produces machine-readable output with summary, functions, coupling, duplicates, dead_code, fragments, boilerplate, and srp sections:
GitHub Actions Annotations
Produces ::warning, ::error, and ::notice annotations that GitHub Actions renders inline on PRs:
::warning file=src/order.rs,line=48::IOSP violation in process_payment: logic=[if (line 50)], calls=[determine_payment_method (line 55)]
::error::Quality analysis: 2 violation(s), 82.3% quality score
DOT (Graphviz)
Generates a call-graph visualization with color-coded nodes:
- Green: Integration
- Blue: Operation
- Red: Violation
- Gray: Trivial
SARIF
Produces SARIF v2.1.0 output for integration with GitHub Code Scanning, VS Code SARIF Viewer, and other static analysis platforms. Includes rules for all dimensions (IOSP, complexity, coupling, DRY, SRP, test quality).
HTML
Generates a self-contained HTML report with:
- Dashboard showing overall quality score and 6 dimension scores
- Collapsible detail sections for IOSP, Complexity, DRY, SRP, Test Quality, and Coupling findings
- Color-coded severity indicators and inline CSS (no external dependencies)
CLI Reference
rustqual [OPTIONS] [PATH]
| Argument / Flag | Description |
|---|---|
PATH |
File or directory to analyze. Defaults to . |
-v, --verbose |
Show all functions, not just findings |
--json |
Output as JSON (shorthand for --format json) |
--format <FORMAT> |
Output format: text, json, github, dot, sarif, html |
-c, --config <PATH> |
Path to config file. Defaults to auto-discovered rustqual.toml |
--strict-closures |
Treat closures as logic (stricter analysis) |
--strict-iterators |
Treat iterator chains (.map, .filter, ...) as logic |
--allow-recursion |
Don't count recursive calls as violations |
--strict-error-propagation |
Count ? operator as logic (implicit control flow) |
--no-fail |
Do not exit with code 1 on quality findings (local exploration) |
--fail-on-warnings |
Treat warnings (e.g. suppression ratio exceeded) as errors (exit 1) |
--init |
Generate a tailored rustqual.toml based on current codebase metrics |
--completions <SHELL> |
Generate shell completions (bash, zsh, fish, elvish, powershell) |
--save-baseline <FILE> |
Save current results as a JSON baseline |
--compare <FILE> |
Compare current results against a saved baseline |
--fail-on-regression |
Exit with code 1 if quality score regressed vs baseline |
--watch |
Watch for file changes and re-analyze continuously |
--suggestions |
Show refactoring suggestions for IOSP violations |
--sort-by-effort |
Sort violations by refactoring effort score (descending) |
--findings |
Show only findings with file:line locations (one per line) |
--min-quality-score <SCORE> |
Exit with code 1 if quality score is below threshold (0–100) |
--diff [REF] |
Only analyze files changed vs a git ref (default: HEAD) |
--coverage <LCOV_FILE> |
Path to LCOV coverage file for test quality analysis (TQ-005) |
--explain <FILE> |
Architecture dimension: print layer assignment, classified imports, and active rules for one file |
Exit Codes
| Code | Meaning |
|---|---|
0 |
Success (no findings, or --no-fail set) |
1 |
Quality findings found (default), regression detected (--fail-on-regression), quality gate breached (--min-quality-score), or warnings present with --fail-on-warnings |
2 |
Configuration error (invalid or unreadable config file) |
Configuration
The analyzer auto-discovers rustqual.toml by searching from the analysis path upward through parent directories. You can also specify a config explicitly with --config. Generate a commented default config with --init.
If a rustqual.toml exists but cannot be parsed (syntax errors, unknown fields), the analyzer exits with code 2 and an error message instead of silently falling back to defaults.
Full rustqual.toml Reference
# ────────────────────────────────────────────────────────────────
# Ignore Functions
# ────────────────────────────────────────────────────────────────
# Functions matching these patterns are completely excluded from analysis.
# Supports full glob syntax: *, ?, [abc], [!abc]
= [
"main", # entry point, always mixes logic + calls
"run", # composition-root dispatcher
"visit_*", # syn::Visit trait implementations (external dispatch)
]
# ────────────────────────────────────────────────────────────────
# Exclude Files
# ────────────────────────────────────────────────────────────────
# Glob patterns for files to exclude from analysis entirely.
= ["examples/**"] # e.g. fixture crates for rule demos
# ────────────────────────────────────────────────────────────────
# Strictness
# ────────────────────────────────────────────────────────────────
= false # If true, closures count as logic
= false # If true, iterator chains count as own calls
= false # If true, recursive calls don't violate IOSP
= false # If true, ? operator counts as logic
# ────────────────────────────────────────────────────────────────
# Suppression Ratio
# ────────────────────────────────────────────────────────────────
# Maximum fraction of functions that may be suppressed (0.0–1.0).
# Exceeding this ratio produces a warning.
= 0.10
# If true, exit with code 1 when warnings are present (e.g. suppression ratio exceeded).
# Default: false. Use --fail-on-warnings CLI flag to enable.
= false
# ────────────────────────────────────────────────────────────────
# Complexity Analysis
# ────────────────────────────────────────────────────────────────
[]
= true
= 15 # Cognitive complexity threshold
= 10 # Cyclomatic complexity threshold
= 4 # Maximum nesting depth before warning
= 60 # Maximum function body lines before warning
= true # Flag numeric literals not in allowed list
= ["0", "1", "-1", "2", "0.0", "1.0"]
= true # Flag functions containing unsafe blocks
= true # Flag unwrap/expect/panic/todo usage
= false # If true, .expect() calls don't trigger warnings
# ────────────────────────────────────────────────────────────────
# Coupling Analysis
# ────────────────────────────────────────────────────────────────
[]
= true
= 0.8 # Instability threshold (Ce / (Ca + Ce))
= 15 # Maximum afferent coupling
= 12 # Maximum efferent coupling
= true # Check Stable Dependencies Principle
# ────────────────────────────────────────────────────────────────
# DRY / Duplicate Detection
# ────────────────────────────────────────────────────────────────
[]
= true
= 50 # Minimum token count for duplicate detection
= 5 # Minimum line count
= 3 # Minimum statements for fragment detection
= 0.85 # Jaccard similarity for near-duplicates
= true # Skip test functions
= true # Enable dead code detection
= true # Flag use foo::* imports
= true # Flag repeated match blocks (DRY-005)
# ────────────────────────────────────────────────────────────────
# Boilerplate Detection
# ────────────────────────────────────────────────────────────────
[]
= true
= true # Suggest derive macros / crates
= [ # Which patterns to check (BP-001 through BP-010)
"BP-001", "BP-002", "BP-003", "BP-004", "BP-005",
"BP-006", "BP-007", "BP-008", "BP-009", "BP-010",
]
# ────────────────────────────────────────────────────────────────
# SRP Analysis
# ────────────────────────────────────────────────────────────────
[]
= true
= 0.6 # Composite score threshold for warnings
= 12 # Maximum struct fields
= 15 # Maximum impl methods
= 10 # Maximum external call targets
= 5 # Maximum function parameters (AST-based)
= 3 # LCOM4 component threshold
= [0.4, 0.25, 0.15, 0.2] # [lcom4, fields, methods, fan_out]
= 300 # Production lines before penalty starts
= 800 # Production lines at maximum penalty
= 2 # Highest allowed (warn on 3+ clusters)
= 5 # Min statements for a function to count in clusters
# ────────────────────────────────────────────────────────────────
# Structural Binary Checks
# ────────────────────────────────────────────────────────────────
[]
= true
= true # Broken Trait Contract (SRP)
= true # Self-less Methods (SRP)
= true # Needless &mut self (SRP)
= true # Orphaned Impl (Coupling)
= true # Single-Impl Trait (Coupling)
= true # Downcast Escape Hatch (Coupling)
= true # Inconsistent Error Types (Coupling)
# ────────────────────────────────────────────────────────────────
# Test Quality Analysis
# ────────────────────────────────────────────────────────────────
[]
= true
= "" # Path to LCOV file (or use --coverage CLI flag)
# Extra macro names (beyond assert*/debug_assert*) to recognize as assertions in TQ-001
# extra_assertion_macros = ["verify", "check", "expect_that"]
# ────────────────────────────────────────────────────────────────
# Quality Weights
# ────────────────────────────────────────────────────────────────
[]
= 0.22
= 0.18
= 0.13
= 0.18
= 0.09
= 0.10
= 0.10
# Weights must sum to 1.0
# ────────────────────────────────────────────────────────────────
# Architecture Dimension (see "Architecture Dimension" section for details)
# ────────────────────────────────────────────────────────────────
[]
= true
[]
= ["domain", "port", "infrastructure", "analysis", "application"]
= "strict_error" # or "composition_root"
[]
= ["src/domain/**"]
[]
= ["src/ports/**"]
[]
= [
"src/adapters/config/**",
"src/adapters/source/**",
"src/adapters/suppression/**",
]
[]
= [
"src/adapters/analyzers/**",
"src/adapters/shared/**",
"src/adapters/report/**",
]
[]
= ["src/app/**"]
[]
= [
"src/lib.rs",
"src/main.rs",
"src/adapters/mod.rs",
"src/bin/**",
"src/cli/**",
"tests/**",
]
# Optional: map external crate names to your own layers (for workspaces)
[]
# "my_domain_crate" = "domain"
# "my_infra_crate" = "infrastructure"
# Forbidden edges (cross-branch imports the layer rule permits but you don't want)
[[]]
= "src/adapters/analyzers/**"
= "src/adapters/report/**"
= "Analyzers produce findings; reporters consume them separately"
# Symbol patterns (see "Architecture Dimension" below for all 7 matcher types)
[[]]
= "no_panic_helpers_in_production"
= ["unwrap", "expect"]
= ["src/**"]
= ["**/tests/**"]
= "Production propagates errors through Result"
[[]]
= "no_syn_in_domain"
= ["syn::", "proc_macro2::", "quote::"]
= ["src/domain/**"]
= "Domain types know no AST representation"
# Trait-signature rule (port contract)
[[]]
= "port_traits"
= "src/ports/**"
= ["shared_ref"]
= ["anyhow::", "Box<dyn"]
= ["syn::", "toml::", "serde_json::"]
= true
= ["Send", "Sync"]
# ────────────────────────────────────────────────────────────────
# Report Aggregation
# ────────────────────────────────────────────────────────────────
[]
= "loc_weighted" # workspace-mode score aggregation
Inline Suppression
To suppress specific findings, add a // qual:allow comment on or immediately before the function definition:
// qual:allow
// qual:allow(iosp) reason: "legacy code, scheduled for refactoring"
// qual:allow(complexity)
// qual:allow(srp)
// #[derive(Debug, Clone)]
Supported dimensions: iosp, complexity, coupling, srp, dry, test_quality.
The legacy // iosp:allow syntax is still supported as an alias for // qual:allow(iosp).
Suppressed functions appear as SUPPRESSED in the output and do not count toward findings. If more than max_suppression_ratio (default 10%) of functions are suppressed, a warning is displayed.
API Annotation
Mark public API functions with // qual:api to exclude them from dead code (DRY-003) and untested function (TQ-003) detection:
// qual:api
// qual:api
Unlike // qual:allow, API markers do not count against the suppression ratio. Use // qual:api for functions that are part of your library's public interface — they have no callers within the project because they're meant to be called by external consumers.
Inverse Annotation
Mark inverse method pairs with // qual:inverse(fn_name) to suppress near-duplicate DRY findings between them:
// qual:inverse(parse)
// qual:inverse(as_str)
Common use cases: serialize/deserialize, encode/decode, to_bytes/from_bytes. Like // qual:api, inverse markers do not count against the suppression ratio — they document intentional structural similarity.
Automatic Leaf Detection
Functions with no own calls (Operations and Trivials) are automatically recognized as leaf functions. Calls to leaves do not count as "own calls" for the caller:
Without leaf detection, cmd_quality would be a Violation (logic + own call). With it, the call to get_config is recognized as terminal — no orchestration involved.
More broadly, calls to any non-Violation function are treated as safe — this includes Operations (pure logic), Trivials (empty/simple), and Integrations (pure delegation). Only calls to other Violations (functions that themselves mix logic and non-safe calls) remain true Violations. This cascades iteratively until stable.
Design note — pragmatic IOSP relaxation: In strict IOSP, any call to an own function from a function with logic constitutes a Violation. rustqual relaxes this: only calls to Violations count as concern-mixing. Calls to well-structured functions (Operations, Integrations, Trivials) are treated as safe building blocks. This eliminates false positives for common patterns while preserving true Violations where tangled code calls other tangled code (e.g., mutually recursive Violations).
Recursive Annotation
Mark intentionally recursive functions with // qual:recursive to prevent the self-call from being counted as an own call:
// qual:recursive
Without the annotation, traverse would be a Violation (loop logic + self-call). With it, the self-call is removed before classification. Like // qual:api and // qual:inverse, recursive markers do not count against the suppression ratio.
Lenient vs. Strict Mode
By default the analyzer runs in lenient mode. This makes it practical for idiomatic Rust code:
| Construct | Lenient (default) | --strict-closures |
--strict-iterators |
|---|---|---|---|
items.iter().map(|x| x + 1) |
ignored entirely | closure logic counted | .map() as own call |
|| { if cond { a } } |
closure logic ignored | if counted as logic |
— |
self.do_work() in closure |
call ignored | call counted as own | — |
x? |
not logic | — | — |
async { if x { } } |
ignored (like closures) | — | — |
Use --strict-error-propagation to count ? as logic.
Features
Quality Score
The overall quality score is a weighted average of seven dimension scores (weights are configurable via [weights] in rustqual.toml):
| Dimension | Default Weight | Metric |
|---|---|---|
| IOSP | 22% | Compliance ratio (non-trivial functions) |
| Complexity | 18% | 1 - (complexity + magic numbers + nesting + length + unsafe + error handling) / total |
| DRY | 13% | 1 - (duplicates + fragments + dead code + boilerplate + wildcards + repeated matches) / total |
| SRP | 18% | 1 - (struct warnings + module warnings + param warnings + structural BTC/SLM/NMS) / total |
| Coupling | 9% | 1 - (coupling warnings + 2×cycles + SDP violations + structural OI/SIT/DEH/IET) / total |
| Test Quality | 10% | 1 - (assertion-free + no-SUT + untested + uncovered + untested-logic) / total |
| Architecture | 10% | 1 - (layer violations + forbidden edges + pattern hits + trait-contract breaches) / total |
Quality score ranges from 0% (all findings) to 100% (no findings). Weights must sum to 1.0.
Quality Gates
By default, the analyzer exits with code 1 on any findings — no extra flags needed for CI. Use --no-fail for local exploration.
# Fail if quality score is below 90%
# Local exploration (never fail)
Violation Severity
Violations are categorized by severity based on the number of findings:
| Severity | Condition |
|---|---|
| Low | ≤2 total findings |
| Medium | 3–5 total findings |
| High | >5 total findings |
Severity is shown as [LOW], [MEDIUM], [HIGH] in text output and as a severity field in JSON/SARIF.
Complexity Metrics
Each analyzed function gets complexity metrics (shown with --verbose):
- cognitive_complexity: Cognitive complexity score (increments for nesting depth)
- cyclomatic_complexity: Cyclomatic complexity score (decision points + 1)
- magic_numbers: Numeric literals not in the configured allowed list
- logic_count: Number of logic occurrences (if, match, operators, etc.)
- call_count: Number of own-function calls
- max_nesting: Maximum nesting depth of control flow
- function_lines: Number of lines in the function body
- unsafe_blocks: Count of
unsafeblocks - unwrap/expect/panic/todo: Error handling pattern counts
Coupling Analysis
Detects module-level coupling issues:
- Afferent coupling (Ca): Modules depending on this one (fan-in)
- Efferent coupling (Ce): Modules this one depends on (fan-out)
- Instability: Ce / (Ca + Ce), ranging from 0.0 (stable) to 1.0 (unstable)
- Circular dependencies: Detected via Kosaraju's iterative SCC algorithm
Leaf modules (Ca=0) are excluded from instability warnings since I=1.0 is natural for them.
- Stable Dependencies Principle (SDP): Flags when a stable module (low instability) depends on a more unstable module. This violates the principle that dependencies should flow toward stability.
DRY Analysis
Detects five categories of repetition:
- Duplicate functions: Exact and near-duplicate functions (via AST normalization + Jaccard similarity)
- Duplicate fragments: Repeated statement sequences across functions (sliding window + merge)
- Dead code: Functions never called from production code, or only called from tests. Detects both direct calls and function references passed as arguments (e.g.,
.for_each(some_fn)). - Boilerplate patterns: 10 common Rust boilerplate patterns (BP-001 through BP-010) including trivial
From/Displayimpls, manual getters/setters, builder patterns, manualDefault, repetitive match arms, error enum boilerplate, and clone-heavy conversions - Wildcard imports: Flags
use foo::*glob imports (excludesprelude::*paths anduse super::*in test modules) - Repeated match patterns (DRY-005): Detects identical
matchblocks (≥3 arms) duplicated across ≥3 instances in ≥2 functions, via AST normalization and structural hashing
SRP Analysis
Detects Single Responsibility Principle violations at three levels:
- Struct-level: LCOM4 cohesion analysis using Union-Find on method→field access graph. Composite score combines normalized LCOM4, field count, method count, and fan-out with configurable weights.
- Module-level (length): Production line counting (before
#[cfg(test)]) with linear penalty between configurable baseline and ceiling. - Module-level (cohesion): Detects files with too many independent function clusters. Uses Union-Find on private substantive functions, leveraging IOSP own-call data. Functions that call each other or share a common caller are united into the same cluster. A file with more than
max_independent_clusters(default 2, so 3+ clusters trigger) independent groups indicates multiple responsibilities that should be split into separate modules.
Structural Binary Checks
Seven binary (pass/fail) checks for common Rust structural issues, integrated into existing dimensions:
| Rule | Name | Dimension | What it checks |
|---|---|---|---|
| BTC | Broken Trait Contract | SRP | Impl blocks missing required trait methods |
| SLM | Self-less Methods | SRP | Methods in impl blocks that don't use self (could be free functions) |
| NMS | Needless &mut self |
SRP | Methods taking &mut self that only read from self |
| OI | Orphaned Impl | Coupling | Impl blocks in files that don't define the implemented type |
| SIT | Single-Impl Trait | Coupling | Traits with exactly one implementation (unnecessary abstraction) |
| DEH | Downcast Escape Hatch | Coupling | .downcast_ref() / .downcast_mut() / .downcast() usage (broken abstraction) |
| IET | Inconsistent Error Types | Coupling | Modules returning 3+ different error types (missing unified error type) |
Each rule can be individually toggled via [structural] config. Suppress with // qual:allow(srp) or // qual:allow(coupling) depending on the dimension.
Architecture Dimension (v1.0)
Four rule types check the structural shape of the codebase against an
explicit layered architecture. Enabled via [architecture] enabled = true.
Layer Rule — files are assigned to layers via path globs; inner layers
(lower rank) may not import from outer layers (higher rank). With
unmatched_behavior = "strict_error", every production file must match
a layer glob; unmatched files become violations. With "composition_root",
unmatched files bypass the rule entirely (useful for Cargo-workspace roots).
A minimal hexagonal layering:
[]
= ["domain", "port", "application", "adapter"]
= "strict_error"
[]
= ["src/domain/**"]
[]
= ["src/ports/**"]
[]
= ["src/app/**"]
[]
= ["src/adapters/**"]
[]
= ["src/lib.rs", "src/main.rs"]
A file in src/domain/** importing from src/adapters/** is flagged.
Rustqual itself uses a five-rank variant that separates
infrastructure-style adapters (config, source, suppression) from
analysis-logic adapters (analyzers, shared, report) — see the
committed rustqual.toml for the full structure.
Forbidden Rule — paired from / to path globs forbid cross-branch
imports:
[[]]
= "src/adapters/analyzers/iosp/**"
= "src/adapters/analyzers/**"
= ["src/adapters/analyzers/iosp/**"]
= "peer analyzers are isolated"
Symbol Patterns — ban specific language shapes via seven matchers:
| Matcher | Hits |
|---|---|
forbid_path_prefix |
any path reference starting with a banned prefix |
forbid_glob_import |
use foo::*; |
forbid_method_call |
x.unwrap() / UFCS Option::unwrap(x) |
forbid_function_call |
Box::new(…) via fully-qualified path |
forbid_macro_call |
panic!(), println!(), etc. |
forbid_item_kind |
async_fn, unsafe_fn, unsafe_impl, static_mut, extern_c_block, inline_cfg_test_module, top_level_cfg_test_item |
forbid_derive |
#[derive(Serialize)] |
Scope is XOR: either allowed_in (whitelist) or forbidden_in (blocklist),
with except as fine-grained overrides. Example:
[[]]
= "no_panic_in_production"
= ["panic", "todo", "unreachable"]
= ["src/**"]
= ["**/tests/**"]
= "production code returns typed errors"
Trait-Signature Rule — structural checks on trait definitions in scope:
[[]]
= "port_traits"
= "src/ports/**"
= ["shared_ref"]
= true
= ["anyhow::", "Box<dyn"]
= ["Send", "Sync"]
= true
Checks: receiver_may_be, methods_must_be_async,
forbidden_return_type_contains, required_param_type_contains,
required_supertraits_contain, must_be_object_safe (conservative: flags
Self returns and method-level generics), forbidden_error_variant_contains.
--explain <FILE> diagnostic mode prints the file's layer assignment,
classified imports, and rule hits — useful for understanding why a rule
fires or when tuning config:
$ cargo run -- --explain src/domain/foo.rs
═══ Architecture Explain: src/domain/foo.rs ═══
Layer: domain (rank 0)
Imports (1):
line 1: crate::adapters::Foo — crate::adapters → layer adapter
Layer violations:
line 1: domain ↛ adapter via crate::adapters::Foo
See examples/architecture/ for a runnable mini-fixture per matcher/rule.
Suppress with // qual:allow(architecture) on the file.
Baseline Comparison
Track quality over time:
# Save current state as baseline
# ... make changes ...
# Compare against baseline (shows new/fixed findings, score delta)
# Fail CI only on regression
The baseline format (v2) includes quality score, all dimension counts, and total findings. V1 baselines (IOSP-only) are still supported for backward compatibility.
Refactoring Suggestions
Provides pattern-based refactoring hints for violations, such as extracting conditions, splitting dispatch logic, or converting loops to iterator chains.
Watch Mode
Monitors the filesystem for .rs file changes and re-runs analysis automatically. Useful during refactoring sessions.
Shell Completions
# Generate completions for your shell
Using with AI Coding Agents
Why AI-Generated Code Needs Structural Analysis
AI coding agents (Claude Code, Cursor, Copilot, etc.) are excellent at producing working code quickly, but they consistently exhibit structural problems that rustqual is designed to catch:
- IOSP violations: AI agents routinely generate functions that mix orchestration with logic — calling helper functions inside
ifblocks, combining validation with dispatch. These "god-functions" are hard to test and hard to maintain. - Complexity creep: Generated functions tend to be long, deeply nested, and full of inline logic rather than composed from small, focused operations.
- Duplication: When asked to implement similar features, AI agents often copy-paste patterns rather than extracting shared abstractions, leading to DRY violations.
- Weak tests: AI-generated tests frequently lack meaningful assertions, contain overly long test functions, or rely heavily on mocks without verifying real behavior. The Test Quality dimension catches assertion-free tests, low assertion density, and coverage gaps.
IOSP is particularly valuable for AI-generated code because it enforces a strict decomposition: every function is either an Integration (orchestrates, no logic) or an Operation (logic, no own calls). This constraint forces the kind of small, testable, single-purpose functions that AI agents tend not to produce on their own.
CLAUDE.md / Cursor Rules Integration
Project-level instruction files (.claude/CLAUDE.md, .cursorrules, etc.) can teach AI agents to follow IOSP principles. Add rules like these to your project:
- ------
This works with any AI tool that reads project-level instruction files. The key insight is that the agent gets actionable feedback: rustqual tells it exactly which function violated which principle, so it can self-correct.
CI Quality Gate for AI-Generated Code
Add rustqual to your CI pipeline so that AI-generated PRs are automatically checked:
name: Quality Check
on:
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo install rustqual cargo-llvm-cov
- name: Generate coverage data
run: cargo llvm-cov --lcov --output-path lcov.info
- name: Check quality (changed files only)
run: rustqual --diff HEAD~1 --coverage lcov.info --fail-on-warnings --format github
Key flags for AI workflows:
--diff HEAD~1— only analyze files changed in the PR, not the entire codebase--coverage lcov.info— include test quality coverage analysis (TQ-005)--fail-on-warnings— treat suppression ratio violations as errors--min-quality-score 90— reject PRs that drop quality below a threshold--format github— produces inline annotations on the PR diff
See CI Integration for more workflow examples including baseline comparison.
Pre-commit Hook
Catch violations before they enter version control — especially useful when AI agents generate code locally:
#!/bin/bash
# .git/hooks/pre-commit
if ! ; then
fi
This gives the AI agent (or developer) immediate feedback before the code is committed. See Pre-commit Hook for the basic setup.
Recommended Workflow
The full quality loop for AI-assisted development:
- Agent instructions — CLAUDE.md / Cursor rules teach the agent IOSP principles and rustqual usage
- Pre-commit hook — catches violations locally before they enter version control
- Coverage verification — generate LCOV data with
cargo llvm-covand pass via--coverageto detect weak or missing tests - CI quality gate — prevents merges below quality threshold using
--min-quality-scoreor--fail-on-regression - Baseline tracking —
--save-baselineand--comparetrack quality score over time, ensuring AI-generated code does not erode structural quality
Architecture
The analyzer uses a two-pass pipeline:
┌──────────────────────────────────┐
│ Pass 1: Collect │
.rs files ──read──► │ Read + Parse all files (rayon) │
│ Build ProjectScope (all names) │
│ Scan for // qual:allow markers │
└────────────────┬─────────────────┘
│
┌────────────────▼─────────────────┐
│ Pass 2: Analyze │
│ For each function: │
│ BodyVisitor walks AST │
│ → logic + call occurrences │
│ → complexity metrics │
│ → classify: I / O / V / T │
│ Coupling analysis (use-graph) │
│ DRY detection (normalize+hash) │
│ SRP analysis (LCOM4+composite) │
│ Compute quality score │
└────────────────┬─────────────────┘
│
┌────────────────▼─────────────────┐
│ Output │
│ Text / JSON / GitHub / DOT / │
│ SARIF / HTML / Suggestions / │
│ Baseline comparison │
└──────────────────────────────────┘
Source Files
~200 production files in src/, ~19 400 lines. Layered per Clean Architecture:
src/
├── lib.rs Composition root (run() entry, ~140 lines)
├── main.rs Thin binary wrapper (rustqual)
├── bin/cargo-qual/main.rs Thin binary wrapper (cargo qual)
│
├── domain/ Pure value types (no syn, no I/O)
│ ├── dimension.rs
│ ├── finding.rs Port-emitted Finding struct
│ ├── score.rs PERCENTAGE_MULTIPLIER
│ ├── severity.rs
│ ├── source_unit.rs
│ └── suppression.rs
│
├── ports/ Trait contracts
│ ├── dimension_analyzer.rs DimensionAnalyzer + AnalysisContext + ParsedFile
│ ├── reporter.rs
│ ├── source_loader.rs
│ └── suppression_parser.rs
│
├── adapters/
│ ├── config/ TOML loading, tailored --init, weight validation
│ ├── source/ Filesystem walk, parse, --watch
│ ├── suppression/ qual:allow marker parsing
│ ├── shared/ Cross-analyzer utilities
│ │ ├── cfg_test.rs has_cfg_test, has_test_attr
│ │ ├── cfg_test_files.rs collect_cfg_test_file_paths
│ │ ├── normalize.rs AST normalization for DRY
│ │ └── use_tree.rs Canonical use-tree walker
│ ├── analyzers/ Seven dimension analyzers
│ │ ├── iosp/ Analyzer, BodyVisitor, classify, scope
│ │ ├── complexity/
│ │ ├── dry/ Incl. boilerplate/ (BP-001–BP-010)
│ │ ├── srp/
│ │ ├── coupling/
│ │ ├── tq/
│ │ ├── structural/ BTC, SLM, NMS, OI, SIT, DEH, IET
│ │ └── architecture/ Layer + Forbidden + Symbol + Trait-contract rules
│ └── report/ Text, JSON, SARIF, HTML, DOT, GitHub,
│ AI, AI-JSON, baseline, suggestions
│
├── app/ Application use cases
│ ├── analyze_codebase.rs Port-based use case
│ ├── pipeline.rs Full-pipeline orchestrator
│ ├── secondary.rs Per-dimension secondary passes
│ ├── metrics.rs Coupling/DRY/SRP helpers
│ ├── tq_metrics.rs
│ ├── structural_metrics.rs
│ ├── architecture.rs Architecture dim wiring via port
│ ├── warnings.rs Complexity + leaf reclass + suppression ratio
│ ├── dry_suppressions.rs
│ ├── exit_gates.rs Default-fail, min-quality, fail-on-warnings
│ └── setup.rs Config loading + CLI overrides
│
└── cli/
├── mod.rs Cli struct (clap), OutputFormat
├── handlers.rs --init, --completions, --save-baseline, --compare
└── explain.rs --explain <file> architecture diagnostic
tests/ Workspace integration tests
├── integration.rs End-to-end CLI invocations
└── showcase_iosp.rs Before/after IOSP refactor demonstration
Companion test trees live next to the production code they cover
(src/<module>/tests/<name>.rs). Workspace-root tests/** are Cargo's
integration-test binaries; each is its own crate.
How Classification Works
- Trivial check: Empty bodies are immediately
Trivial. Single-statement bodies are analyzed — only classified as Trivial if they contain neither logic nor own calls. - AST walking:
BodyVisitorimplementssyn::visit::Visitto walk the function body, recording:- Logic:
if,match,for,while,loop, binary operators (+,&&,>, etc.), optionally?operator - Own calls: function/method calls that match names defined in the project (via
ProjectScope) - Nesting depth: tracks control-flow nesting for complexity metrics
- Logic:
- Classification:
- Logic only → Operation
- Own calls only → Integration
- Both → Violation (with severity based on finding count)
- Neither → Trivial
- Recursion exception: If
allow_recursionis enabled and the only own call is to the function itself, it's classified as Operation instead of Violation.
ProjectScope: Solving the Method Call Problem
Without type information, the analyzer cannot distinguish self.push(x) (Vec method, external) from self.analyze(x) (own method). The ProjectScope solves this with a two-pass approach:
- First pass: Scan all
.rsfiles and collect every declared function, method, struct, enum, and trait name. - Second pass: During analysis, a call is only counted as "own" if the name exists in the project scope.
This means v.push(1) is never counted as own (since push is not defined in your project), while self.analyze_file(f) is (because analyze_file is defined in your project).
Universal methods (~26 entries like new, default, fmt, clone, eq, ...) are always treated as external, even if your project implements them via trait impls. This prevents false positives from standard trait implementations.
IOSP Score
IOSP Score = (Integrations + Operations) / (Integrations + Operations + Violations) × 100%
Trivial and suppressed functions are excluded because they are too small or explicitly allowed.
CI Integration
GitHub Actions
name: Quality Check
on:
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Install rustqual
run: cargo install --path .
- name: Check code quality
run: rustqual src/ --min-quality-score 90 --format github
GitHub Actions with Baseline
- name: Check quality regression
run: |
rustqual src/ --compare baseline.json --fail-on-regression --format github
Generic CI (JSON)
- name: Quality Check
run: |
cargo run --release -- src/ --json > quality-report.json
cat quality-report.json
Pre-commit Hook
#!/bin/bash
# .git/hooks/pre-commit
if ! ; then
fi
How to Fix Violations
When a function is flagged as a violation, refactor by splitting it into pure integrations and operations:
Before (violation):
After (IOSP compliant):
// Integration: orchestrates, no logic
// Operation: logic only, no own calls
// Integration: delegates to transform or default
Common refactoring patterns:
| Pattern | Approach |
|---|---|
if + call in branch |
Extract the condition into an Operation, use .then() or pass result to Integration |
for loop with calls |
Use iterator chains (.iter().map(|x| process(x)).collect()) — closures are lenient |
| Match + calls | Extract match logic into an Operation that returns an enum/value, dispatch in Integration |
Use --suggestions to get automated refactoring hints.
Self-Compliance
rustqual analyzes itself with zero findings across all seven dimensions:
)
This is verified by the integration test suite and CI. Note: use . as
the analysis root (not src/) so that architecture-rule globs like
src/adapters/** match the actual paths.
Testing
RUSTFLAGS="-Dwarnings"
The test suite covers:
- adapters/analyzers/ — classification, closures, iterators, scope, recursion,
?operator, async/await, severity, complexity, IOSP/DRY/SRP/coupling/TQ/structural/architecture rule behaviour - adapters/config/ — ignore patterns, glob compilation, TOML loading, validation, tailored
--initgeneration, weight sum check - adapters/report/ — summary stats, JSON structure, suppression counting, baseline roundtrip, HTML, SARIF, GitHub annotations, AI/TOON output
- adapters/shared/ — cfg-test detection, use-tree walking, AST normalization
- adapters/source/ — filesystem walk,
--watchloop - app/ — pipeline orchestration, exit gates, setup, secondary-pass coordination, warning accumulation
- domain/ + ports/ — value-type invariants and trait-contract shape
- Integration tests (
tests/integration.rs): self-analysis, sample expectations, JSON validity, verbose output - Showcase tests (
tests/showcase_iosp.rs): before/after IOSP refactoring examples
Known Limitations
- Syntactic analysis only: Uses
synfor AST parsing without type resolution. Cannot determine the receiver type of method calls — relies onProjectScopeheuristics andexternal_prefixesconfig as fallbacks. - Macros: Macro invocations are not expanded.
println!etc. are handled as special cases viaexternal_prefixes, but custom macros producing logic or calls may be misclassified. - External file modules:
mod foo;declarations pointing to separate files are not followed. Only inline modules (mod foo { ... }) are analyzed recursively. - Parallelization: The analysis pass is sequential because
proc_macro2::Span(withspan-locationsenabled for line numbers) is notSync. File I/O is parallelized viarayon.
Dependencies
| Crate | Purpose |
|---|---|
syn |
Rust AST parsing (with full, visit features) |
proc-macro2 |
Span locations for line numbers |
quote |
Token stream formatting (generic type display) |
derive_more |
Display derive for analysis types |
clap |
CLI argument parsing |
clap_complete |
Shell completion generation |
walkdir |
Recursive directory traversal |
colored |
Terminal color output |
serde |
Config deserialization |
toml |
TOML config file parsing |
serde_json |
JSON output serialization |
globset |
Glob pattern matching for ignore/exclude |
rayon |
Parallel file I/O |
notify |
File system watching for --watch mode |
License
MIT