Rust Lint
Purpose
Run cargo build and cargo clippy to surface compiler and linter warnings, then fix them systematically.
Procedure
1. Gather Warnings
Run both commands and capture all output:
bashcargo build 2>&1 cargo clippy --all-targets 2>&1
If the project uses a workspace, warnings will cover all crates. If you only want to lint a specific crate:
bashcargo clippy -p <crate-name> --all-targets 2>&1
2. Categorize Warnings
Group warnings into categories:
- Unused imports / variables: Remove or prefix with
_. - Dead code: Remove unused functions, structs, or fields. If they are part of a public API, document them with
#[allow(dead_code)]only if genuinely needed. - Needless lifetimes: Remove explicit lifetime annotations that the compiler can infer.
- Clippy style warnings: Apply the suggested fix (e.g.,
unwrap_or_default(),is_empty(),len() == 0→is_empty()). - Missing documentation: Add doc comments to public items.
- Improper regex / format strings: Fix as suggested.
3. Fix Warnings
For each warning:
- Read the surrounding code using the
readtool to understand context. - Apply the fix using
editormultiedit. - Re-run the specific check to confirm the warning is resolved:
bashcargo clippy -p <crate-name> --all-targets 2>&1 | grep -A 5 "<warning pattern>"
4. Verify
After all fixes:
- Run
cargo buildand confirm no warnings remain. - Run
cargo clippy --all-targetsand confirm no warnings remain. - Run
cargo testto ensure nothing broke. - Run
cargo fmt --checkto ensure formatting is clean.
Guidelines
- Fix one category of warnings at a time to keep changes reviewable.
- Never suppress a warning with
#[allow(...)]unless there is a justified reason — document it in a comment. - Prefer clippy's suggested fixes when available.
- If a warning is a false positive, add a targeted
#[allow(...)]with a comment explaining why. - Keep commits focused: one logical fix per commit (e.g., "fix: remove unused imports").
- Do not introduce new dependencies to silence warnings.
- If a warning originates from a dependency, consider upgrading or ignoring it via
clippy.tomlor#![allow]at the crate level.


