Code Quality
Automated collection of code quality metrics — unsafe code detection, binary size analysis, and documentation coverage — emitted as a single report artifact.
Reference
Section titled “Reference”| Field | Value |
|---|---|
| Workflow | .github/workflows/code-quality.yml |
| Tools | cargo-geiger, cargo-bloat, rustdoc |
| Output | Markdown report artifact |
Metrics collected
Section titled “Metrics collected”| Metric | Tool | What it detects |
|---|---|---|
| Unsafe code analysis | cargo-geiger | Unsafe function calls, blocks, trait impls, and unsafe in dependencies |
| Binary size analysis | cargo-bloat | Size by crate and by function; bloat sources |
| Documentation coverage | rustdoc | Missing doc comments, broken doc links, doc test failures |
Report access
Section titled “Report access”The workflow generates a combined report. Access it via Actions → Code Quality Metrics → Artifacts → code-quality-metrics.
Example report:
## Unsafe Code AnalysisFunctions Expressions Impls Traits Methods Dependency0/10 0/100 0/5 0/2 0/20 rust_template
## Binary Size AnalysisFile .text Size Crate 71.0% 59.0% 1.2MiB std 8.5% 7.1% 147KiB rust_template
## Documentation CoverageDocumenting rust_template v0.1.0warning: missing documentation for public functionInterpreting the unsafe code report
Section titled “Interpreting the unsafe code report”Functions Expressions Impls Traits Methods Dependency2/10 5/100 0/5 0/2 0/20 ✓ rust_template- Functions: 2 functions contain unsafe code.
- Expressions: 5 unsafe expressions total.
- ✓ = no unsafe in this crate’s API.
Interpreting the binary size report
Section titled “Interpreting the binary size report”File .text Size Crate71.0% 59.0% 1.2MiB std ← Standard library 8.5% 7.1% 147KiB rust_template 5.2% 4.3% 89KiB serdeA large dependency contribution (e.g. serde) is a candidate for feature-flag trimming.
Interpreting documentation coverage
Section titled “Interpreting documentation coverage”warning: missing documentation for public function `add` --> crates/lib.rs:10Each warning names the public item that needs a doc comment.
How-to
Section titled “How-to”Run the analysis locally
Section titled “Run the analysis locally”# Install toolscargo install cargo-geiger cargo-bloat
# Run unsafe code analysiscargo geiger --all-features
# Analyze binary sizecargo build --releasecargo bloat --release --crates
# Check documentationcargo doc --no-deps --all-featuresVerify: each command prints a report section matching the formats above.
Configure unsafe code policy
Section titled “Configure unsafe code policy”Set the unsafe policy in Cargo.toml:
[lints.rust]unsafe_code = "forbid" # No unsafe allowed# orunsafe_code = "warn" # Warn but allowVerify: cargo geiger reports 0/N for a forbid crate.
Configure binary size optimization
Section titled “Configure binary size optimization”[profile.release]opt-level = "z" # Optimize for sizelto = true # Link-time optimizationcodegen-units = 1 # Better optimizationstrip = true # Remove symbolspanic = "abort" # Smaller panic handlerVerify: cargo build --release && cargo bloat --release --crates and compare the total size.
Configure documentation requirements
Section titled “Configure documentation requirements”[lints.rust]missing_docs = "warn" # Warn on missing docsrustdoc::broken_intra_doc_links = "deny" # Fail on broken linksVerify: cargo doc --no-deps surfaces missing-doc warnings.
Improve the metrics
Section titled “Improve the metrics”Reduce unsafe code — replace raw pointer writes with safe abstractions:
// Beforeunsafe { *ptr = value;}
// After - use safe abstractionvec[index] = value;Reduce binary size — find the largest contributors, then enable size optimizations:
cargo bloat --release -n 20Improve documentation — add the missing doc comment the report named:
/// Adds two numbers together.pub fn add(a: i64, b: i64) -> i64 { a + b}cargo doc --no-deps # Check coveragecargo test --doc # Run doc testscargo doc --no-deps --document-private-items # Generate private docsVerify: re-run the relevant tool and confirm the count dropped.
Troubleshooting
Section titled “Troubleshooting”cargo-geiger errors:
cargo install cargo-geiger --forcecargo cleancargo geigerBinary size analysis fails — ensure a release build exists first:
cargo build --releasecargo bloat --releaseDocumentation warnings — surface them all as errors:
RUSTDOCFLAGS="-D warnings" cargo doc --no-depsWhy this matters
Section titled “Why this matters”These three metrics each guard a property the compiler alone won’t enforce. unsafe_code = "forbid" is the template default, so cargo-geiger exists to confirm that guarantee holds transitively — unsafe code bypasses Rust’s safety model, and a dependency can reintroduce it silently. Binary size matters for download time and memory footprint, and cargo-bloat makes the cost of each dependency visible so feature trimming is an informed decision. Documentation coverage is a usability property: a well-documented public API is the difference between a crate people can adopt and one they have to reverse-engineer.