| name | fix-clippy-warnings |
| description | Guide for fixing Rust Clippy warnings in the torrust-tracker project. Covers proper application of clippy suggestions, when to add allowances, and how to document exceptions. Use when asked to fix clippy warnings, improve code quality, or resolve linter issues. Triggers on "fix clippy", "clippy warnings", "rust code quality", or "linting issues". |
| metadata | {"author":"torrust","version":"1.0"} |
Fix Clippy Warnings
This skill guides you through the proper handling of Rust Clippy warnings in the Torrust Tracker project.
Clippy Philosophy
Always prefer fixing clippy warnings with the suggested approach rather than adding #[allow(...)] attributes. Clippy warnings are designed to improve code quality, readability, and maintainability.
When to Apply Clippy Suggestions
✅ Apply Suggested Fixes
When clippy suggests a specific code change that improves quality:
- Use
as_chunks::<N>() instead of chunks_exact(N) (as we did in SI-4)
- Use
#[allow(clippy::explicit_iter_loop)] instead of iter() when it's more concise
- Apply any other suggestion that improves code quality
⚠️ When to Add Allowances
Only add #[allow(...)] when:
- The suggestion is not applicable to the specific use case
- The suggestion would break existing functionality or API
- The suggestion is temporarily ignored during a refactoring phase
- The suggestion is not yet supported in the current Rust version
How to Document Exceptions
When adding #[allow(...)] attributes, always include a clear comment explaining why:
#[allow(clippy::unnecessary_wraps)]
fn parse_announce_response(data: &[u8]) -> Result<Response, ParseError> {
}
Common Clippy Patterns
Pattern 1: chunks_exact → as_chunks
Before:
for chunk in bytes.chunks_exact(6) {
}
After:
let (chunks, remainder) = bytes.as_chunks::<6>();
if !remainder.is_empty() {
return Err(ParseError::InvalidChunkSize);
}
for chunk in chunks.iter() {
}
Pattern 2: Explicit Iterator Loop
Before:
for item in items.iter() {
}
After:
for item in &items {
}
Clippy Workflow
- Identify the warning: Run
linter clippy to see specific clippy errors
- Apply suggestion: Try the suggested fix first
- Verify functionality: Ensure the change doesn't break existing behavior
- Document exceptions: Add clear comments for any allowances
- Run full linters: Confirm
linter all passes
Related Skills