Rust Project Guardrails
Enforce Rust-specific project standards including Cargo manifest presence, lockfile requirements, edition and MSRV minimums, unsafe block limits, and clippy compliance.
rust to your lunar-config.yml:uses: github://earthly/lunar-lib/policies/rust@v1.0.0
Included Guardrails
This policy includes 6 guardrails that enforce standards for your devex build and ci.
cargo-toml-exists
Ensures the project has a Cargo.toml file for crate configuration. Required for all Rust projects.
cargo-lock-exists
Ensures the project has a Cargo.lock file for dependency pinning. By default required for applications, skipped for libraries (following Cargo conventions). Configurable via lock_mode.
min-rust-edition
Ensures the project uses at least the minimum required Rust edition. Editions enable new language features while preserving backward compatibility.
min-rust-version-cicd
Ensures the Rust toolchain version used in CI/CD commands meets the minimum required version. Helps maintain security and compatibility standards for build environments.
clippy-clean
Ensures clippy reports no warnings (or fewer than the configured threshold). Clippy catches common mistakes and enforces idiomatic Rust patterns.
max-unsafe-blocks
Limits the number of unsafe blocks in the codebase. Unsafe code bypasses Rust's safety guarantees and should be minimized and carefully reviewed.
How Guardrails Fit into Lunar
Lunar guardrails define your engineering standards as code. They evaluate data collected by integrations and produce pass/fail checks with actionable feedback.
Policies support gradual enforcement—from silent scoring to blocking PRs or deployments—letting you roll out standards at your own pace without disrupting existing workflows.
Learn How Lunar Works →Required Integrations
This policy evaluates data gathered by one or more of the following integration(s).
Make sure to enable them in your lunar-config.yml.
Configuration
Configure this policy in your lunar-config.yml.
Inputs
| Input | Required | Default | Description |
|---|---|---|---|
lock_mode
|
Optional |
auto
|
Cargo.lock enforcement mode. The "auto" mode detects whether the crate is a library or application and applies the Cargo convention (required for apps, skipped for libs). - "auto": Require for applications, skip for libraries (default) - "required": Always require Cargo.lock - "forbidden": Fail if Cargo.lock exists - "none": Skip the check entirely |
min_rust_edition
|
Optional |
2021
|
Minimum required Rust edition (e.g., "2021", "2024") |
min_rust_version_cicd
|
Optional |
1.75.0
|
Minimum required Rust toolchain version for CI/CD (e.g., "1.75.0") |
max_clippy_warnings
|
Optional |
0
|
Maximum allowed clippy warnings (0 = must be clean) |
max_unsafe_blocks
|
Optional |
0
|
Maximum allowed unsafe blocks (0 = no unsafe allowed) |
Documentation
View on GitHubRust Project Guardrails
Enforce Rust-specific project standards including Cargo manifest presence, lockfile requirements, edition and MSRV minimums, unsafe block limits, and clippy compliance.
Overview
This policy validates Rust projects against best practices for crate management, safety, and code quality. It ensures projects have proper Cargo.toml and Cargo.lock files, use a modern Rust edition, limit unsafe code, and pass clippy linting.
Policies
This plugin provides the following policies (use include to select a subset):
| Policy | Description | Failure Meaning |
|---|---|---|
cargo-toml-exists |
Validates Cargo.toml exists | Project lacks crate manifest |
cargo-lock-exists |
Validates Cargo.lock for applications | Missing dependency lockfile |
min-rust-edition |
Ensures minimum Rust edition | Edition too old |
min-rust-version-cicd |
Ensures minimum Rust version in CI/CD | CI/CD Rust toolchain too old |
clippy-clean |
Ensures no clippy warnings | Clippy found code quality issues |
max-unsafe-blocks |
Limits unsafe block count | Too many unsafe blocks |
Required Data
This policy reads from the following Component JSON paths:
| Path | Type | Provided By |
|---|---|---|
.lang.rust |
object | rust collector |
.lang.rust.cargo_toml_exists |
boolean | rust collector |
.lang.rust.cargo_lock_exists |
boolean | rust collector |
.lang.rust.edition |
string | rust collector |
.lang.rust.is_application |
boolean | rust collector |
.lang.rust.is_library |
boolean | rust collector |
.lang.rust.unsafe_blocks |
object | rust collector |
.lang.rust.cicd.cmds |
array | rust collector |
.lang.rust.lint.warnings |
array | rust collector |
Installation
Add to your lunar-config.yml:
policies:
- uses: github://earthly/lunar-lib/policies/rust@main
on: [rust] # Or use tags like ["domain:backend"]
enforcement: report-pr
# include: [cargo-toml-exists, cargo-lock-exists] # Only run specific checks
with:
lock_mode: "auto" # "auto", "required", "forbidden", "none" (default: "auto")
min_rust_edition: "2021" # Minimum Rust edition (default: "2021")
min_rust_version_cicd: "1.75.0" # Minimum Rust version in CI (default: "1.75.0")
max_clippy_warnings: "0" # Maximum clippy warnings allowed (default: "0")
max_unsafe_blocks: "0" # Maximum unsafe blocks allowed (default: "0")
Examples
Passing Example
{
"lang": {
"rust": {
"edition": "2021",
"cargo_toml_exists": true,
"cargo_lock_exists": true,
"is_application": true,
"is_library": false,
"unsafe_blocks": {
"count": 0,
"locations": []
},
"lint": {
"warnings": []
},
"cicd": {
"cmds": [
{ "cmd": "cargo test", "version": "1.77.0" }
]
}
}
}
}
Failing Example
{
"lang": {
"rust": {
"edition": "2018",
"cargo_toml_exists": true,
"cargo_lock_exists": false,
"is_application": true,
"is_library": false,
"unsafe_blocks": {
"count": 5,
"locations": [
{"file": "src/main.rs", "line": 10},
{"file": "src/ffi.rs", "line": 22},
{"file": "src/ffi.rs", "line": 45},
{"file": "src/ffi.rs", "line": 78},
{"file": "src/lib.rs", "line": 100}
]
},
"lint": {
"warnings": [
{ "file": "src/main.rs", "line": 5, "message": "unused variable: `x`", "lint": "unused_variables" }
]
}
}
}
}
Failure messages:
"Cargo.lock not found. Applications should commit Cargo.lock for reproducible builds. Run 'cargo generate-lockfile' to create it.""Rust edition 2018 is below minimum 2021. Update edition in Cargo.toml to '2021' or later.""5 unsafe blocks found, maximum allowed is 0. Reduce unsafe usage or increase the max_unsafe_blocks threshold.""1 clippy warning(s) found, maximum allowed is 0. Run 'cargo clippy' and fix all warnings."
Remediation
cargo-toml-exists
- Run
cargo initto create a Cargo.toml file - Or
cargo new <project-name>for a new project
cargo-lock-exists
- Run
cargo generate-lockfileto create Cargo.lock - Commit it to version control (for applications)
- For libraries, keep
lock_mode: "auto"(default) — it already skips the check for library crates. Uselock_mode: "none"to disable the check entirely regardless of crate type
min-rust-edition
- Update
editionin Cargo.toml:edition = "2021" - Run
cargo fix --editionto automatically migrate code - Test thoroughly after edition migration
min-rust-version-cicd
- Update your CI/CD pipeline to use a newer Rust toolchain
- For GitHub Actions: update
toolchainindtolnay/rust-toolchain - Update
rust-toolchain.tomlto pin the desired version
clippy-clean
- Run
cargo clippyto see all warnings - Fix issues or apply suggested fixes with
cargo clippy --fix - For false positives, add
#[allow(clippy::lint_name)]with a comment explaining why
max-unsafe-blocks
- Review each unsafe block for necessity
- Consider using safe abstractions or crates that encapsulate unsafe code
- If unsafe is necessary, document the safety invariants with
// SAFETY:comments - If more unsafe is needed, increase
max_unsafe_blocksthreshold with justification
Open Source
This policy is open source and available on GitHub. Contribute improvements, report issues, or fork it for your own use.
Common Use Cases
Explore how individual guardrails work with specific integrations.
Ready to Automate Your Standards?
See how Lunar can turn your engineering wiki, compliance docs, or postmortem action items into automated guardrails with our 100+ built-in guardrails.