Make unused imports fail builds instead of just warning
Problem to Solve
Currently, unused imports in Rust code only generate warnings during compilation and linting, but don't fail the build. This can lead to:
- Code bloat: Accumulation of unnecessary import statements over time
- Reduced readability: Cluttered import sections that don't reflect actual dependencies
- Maintenance overhead: Developers need to manually clean up unused imports
- Inconsistent code quality: Some files may have clean imports while others accumulate unused ones
The project already has a robust linting setup with Clippy configured to fail on warnings (-D warnings), but the standard unused_imports lint is only a warning by default.
Current State
From the existing configuration:
- Clippy is configured with
-D warningsinmise.toml - CI runs
cargo clippy --all-targets --all-features --features no-frontend -- -D warnings - There's one existing
#[allow(unused_imports)]in test code that appears to be intentional
Proposed Solution
Configure the Rust compiler to treat unused imports as errors instead of warnings by adding the following lint configuration to the workspace Cargo.toml:
[workspace.lints.rust]
unused_imports = "deny"
This approach:
- Fails builds when unused imports are detected
- Applies consistently across all workspace crates
- Integrates seamlessly with existing CI/linting infrastructure
-
Allows exceptions where needed using
#[allow(unused_imports)]
Implementation Details
-
Add lint configuration to the workspace
Cargo.toml - Clean up existing unused imports (if any) to ensure builds pass
- Verify CI integration - the existing Clippy configuration should automatically pick up the new lint level
- Update documentation if needed to mention the stricter import policy
Benefits
- Enforced code cleanliness: Prevents accumulation of unused imports
- Better developer experience: Clear feedback when imports are unnecessary
- Consistent codebase: All crates follow the same import hygiene standards
- Automated enforcement: No manual review needed for import cleanliness
Alternative Approaches Considered
-
Clippy-only approach: Could use
clippy::unused_importsbut the standardunused_importslint is sufficient - Pre-commit hooks: Could add import cleanup to git hooks, but build-time enforcement is more reliable
- Manual enforcement: Relying on code review, but automated enforcement is more consistent
This change aligns with the project's existing focus on code quality and automated linting, extending the "warnings as errors" philosophy to import hygiene.