| created | "2025-12-16T00:00:00.000Z" |
| modified | "2026-05-09T00:00:00.000Z" |
| reviewed | "2026-04-25T00:00:00.000Z" |
| name | cargo-llvm-cov |
| description | cargo-llvm-cov: Rust code coverage with LLVM instrumentation. Use when measuring coverage, enforcing thresholds, generating reports, or integrating codecov/coveralls. |
| user-invocable | false |
| allowed-tools | Bash, Read, Grep, Glob |
cargo-llvm-cov - Code Coverage with LLVM
cargo-llvm-cov provides accurate code coverage for Rust using LLVM's instrumentation-based coverage. It supports multiple output formats and integrates seamlessly with CI platforms.
When to Use This Skill
| Use this skill when... | Use sibling skill instead when... |
|---|
| Measuring line / branch coverage with LLVM instrumentation | Just running tests fast in parallel -- use cargo-nextest |
| Generating HTML / lcov / codecov reports | Auditing unused dependencies -- use cargo-machete |
| Enforcing coverage thresholds in CI | Configuring lint rules -- use clippy-advanced |
| Wiring codecov.io or coveralls upload | Writing the tests themselves -- use rust-development |
Installation
cargo install cargo-llvm-cov
cargo llvm-cov --version
rustup component add llvm-tools-preview
Basic Usage
cargo llvm-cov
cargo llvm-cov --html
open target/llvm-cov/html/index.html
cargo llvm-cov --lcov --output-path target/llvm-cov/lcov.info
cargo llvm-cov --json --output-path target/llvm-cov/coverage.json
cargo llvm-cov --text
cargo llvm-cov --text -- --show-instantiations
Output Formats
cargo llvm-cov --html --open
cargo llvm-cov --lcov --output-path lcov.info
cargo llvm-cov --json --output-path coverage.json
cargo llvm-cov --cobertura --output-path cobertura.xml
cargo llvm-cov --text
cargo llvm-cov --html --lcov --output-path lcov.info
Coverage Thresholds for CI
cargo llvm-cov --fail-under-lines 80
cargo llvm-cov --fail-under-lines 80 --fail-under-functions 75
cargo llvm-cov --fail-under-lines 80
cargo llvm-cov --fail-under-regions 80
cargo llvm-cov --fail-under-functions 75
Threshold Configuration
Create a script or Makefile for consistent thresholds:
.PHONY: coverage coverage-ci
coverage:
cargo llvm-cov --html --open
coverage-ci:
cargo llvm-cov \
--fail-under-lines 80 \
--fail-under-functions 75 \
--lcov --output-path lcov.info
Or use a shell script:
#!/usr/bin/env bash
set -euo pipefail
COVERAGE_THRESHOLD="${COVERAGE_THRESHOLD:-80}"
cargo llvm-cov \
--fail-under-lines "$COVERAGE_THRESHOLD" \
--lcov --output-path target/llvm-cov/lcov.info \
--html
echo "Coverage threshold: $COVERAGE_THRESHOLD% (lines)"
Branch Coverage (Nightly)
Branch coverage requires Rust nightly:
rustup toolchain install nightly
rustup component add llvm-tools-preview --toolchain nightly
cargo +nightly llvm-cov --branch --html
cargo +nightly llvm-cov \
--branch \
--fail-under-lines 80 \
--fail-under-branches 70
Branch Coverage Configuration
[toolchain]
channel = "nightly"
components = ["llvm-tools-preview"]
Integration with cargo-nextest
cargo llvm-cov nextest --html
cargo llvm-cov nextest --profile ci --lcov --output-path lcov.info
cargo llvm-cov nextest -E 'not test(slow_)' --html
Codecov Integration
name: Coverage
on: [push, pull_request]
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@v2
with:
tool: cargo-llvm-cov
- name: Generate coverage
run: cargo llvm-cov --all-features --lcov --output-path lcov.info
- name: Upload to codecov
uses: codecov/codecov-action@v4
with:
files: lcov.info
fail_ci_if_error: true
token: ${{
Coveralls Integration
name: Coverage
on: [push, pull_request]
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@v2
with:
tool: cargo-llvm-cov
- name: Generate coverage
run: cargo llvm-cov --all-features --lcov --output-path lcov.info
- name: Upload to Coveralls
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: lcov.info
Advanced Configuration
Exclude Files from Coverage
cargo llvm-cov --ignore-filename-regex '.*generated.*'
cargo llvm-cov --ignore-filename-regex '.*test.*'
cargo llvm-cov \
--ignore-filename-regex '.*generated.*' \
--ignore-filename-regex '.*mock.*'
Workspace Coverage
cargo llvm-cov --workspace --html
cargo llvm-cov -p my_lib -p my_app --html
cargo llvm-cov --workspace --exclude integration_tests --html
Coverage with Feature Flags
cargo llvm-cov --all-features --html
cargo llvm-cov --features async,tls --html
cargo llvm-cov --no-default-features --html
GitHub Actions: Complete Example
name: Coverage
on:
push:
branches: [main]
pull_request:
env:
CARGO_TERM_COLOR: always
COVERAGE_THRESHOLD: 80
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview
- uses: Swatinem/rust-cache@v2
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@v2
with:
tool: cargo-llvm-cov
- name: Install cargo-nextest
uses: taiki-e/install-action@v2
with:
tool: nextest
- name: Generate coverage
run: |
cargo llvm-cov nextest \
--all-features \
--fail-under-lines $COVERAGE_THRESHOLD \
--lcov --output-path lcov.info \
--html
Clean Coverage Data
cargo llvm-cov clean
cargo llvm-cov clean && cargo llvm-cov --html
Doctests Coverage
cargo llvm-cov --doc --html
cargo llvm-cov --workspace --doc --html
Comparison with tarpaulin
| Feature | cargo-llvm-cov | cargo-tarpaulin |
|---|
| Backend | LLVM (compiler) | ptrace (runtime) |
| Accuracy | Excellent | Good |
| Performance | Fast | Slower |
| Branch coverage | Yes (nightly) | Yes |
| Stability | Stable | Sometimes unstable |
| Platform support | All Rust targets | Linux, limited macOS |
| Setup complexity | Simple | Simple |
Best Practices
-
Use with nextest for faster execution:
cargo llvm-cov nextest --all-features --html
-
Enforce coverage thresholds in CI:
cargo llvm-cov --fail-under-lines 80
-
Generate multiple formats for different tools:
cargo llvm-cov --html --lcov --output-path lcov.info
-
Exclude generated code from coverage:
cargo llvm-cov --ignore-filename-regex '.*generated.*'
-
Cache coverage dependencies in CI:
- uses: Swatinem/rust-cache@v2
-
Use branch coverage on nightly for critical code:
cargo +nightly llvm-cov --branch --fail-under-branches 70
-
Clean coverage data between runs:
cargo llvm-cov clean
Troubleshooting
Missing llvm-tools-preview:
rustup component add llvm-tools-preview
Coverage seems inaccurate:
cargo llvm-cov clean
cargo clean
cargo llvm-cov --html
Nightly features not working:
rustup toolchain install nightly
rustup component add llvm-tools-preview --toolchain nightly
cargo +nightly llvm-cov --branch --html
Slow coverage generation:
cargo llvm-cov nextest --html
Doctests not included:
cargo llvm-cov --doc --html
References