| name | rust-clippy |
| description | Use Clippy lints effectively, understand common warnings, and apply appropriate fixes. Use when running clippy, fixing lint warnings, understanding when to allow/deny lints, or improving code quality. Handles common lints, performance suggestions, style improvements, and lint configuration. |
Clippy Linting
Guidelines for using Clippy effectively to improve Rust code quality.
When to Use This Skill
- Running
cargo clippy and fixing warnings
- Understanding what Clippy warnings mean
- Deciding when to allow/deny specific lints
- Improving code quality and performance
- Configuring Clippy for a project
Running Clippy
Basic Commands
cargo clippy
cargo clippy --all-targets --all-features
cargo clippy --fix --allow-dirty
cargo clippy -- -D warnings
cargo clippy -- -A clippy::lint_name
cargo clippy -- -D clippy::lint_name
Common Clippy Lints
Performance Lints
needless_collect
let items: Vec<_> = iterator.collect();
for item in items.iter() {
process(item);
}
for item in iterator {
process(item);
}
unnecessary_to_owned
let owned = string.to_owned();
let borrowed = &string;
redundant_clone
let cloned = vec.clone();
process(cloned);
process(&vec);
Style Lints
single_char_pattern
if s.contains("x") { }
if s.contains('x') { }
len_zero
if vec.len() == 0 { }
if vec.is_empty() { }
bool_comparison
if condition == true { }
if condition { }
Correctness Lints
unwrap_used
let value = option.unwrap();
let value = option.ok_or(Error::MissingValue)?;
expect_used
let value = option.expect("failed");
let value = option.expect("Failed to get value: context");
panic
if condition {
panic!("error");
}
if condition {
return Err(Error::InvalidInput);
}
Complexity Lints
too_many_arguments
fn process(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) { }
struct Params {
a: i32,
b: i32,
c: i32,
d: i32,
e: i32,
f: i32,
}
fn process(params: Params) { }
cognitive_complexity
fn complex() {
if a {
if b {
if c {
if d {
}
}
}
}
}
fn complex() {
if a && b && c && d {
handle_case();
}
}
Allowing/Denying Lints
In Code
#[allow(clippy::lint_name)]
fn function() {
}
#[deny(clippy::lint_name)]
mod module {
}
In Configuration
[lints.clippy]
unwrap_used = "deny"
expect_used = "warn"
In Command Line
cargo clippy -- -A clippy::unwrap_used
cargo clippy -- -D clippy::unwrap_used
Clippy Configuration
clippy.toml
avoid-breaking-exported-api = false
too-many-arguments-threshold = 10
In Cargo.toml
[lints.clippy]
unwrap_used = "deny"
expect_used = "deny"
Important Rules
- Fix warnings when possible: Don't suppress without good reason
- Use meaningful allow comments: Explain why lint is allowed
- Consider performance lints: They often improve code efficiency
- Don't ignore correctness lints: They catch real bugs
- Use
--fix carefully: Review auto-fixes before committing
- Configure project-wide: Set lint levels in
Cargo.toml or clippy.toml
Common Patterns
✅ Good
let sum: i32 = items.iter().sum();
if vec.is_empty() { }
let value = option.ok_or(Error::Missing)?;
❌ Avoid
let vec: Vec<_> = iterator.collect();
for item in vec.iter() { }
let value = option.unwrap();
if condition == true { }
Examples from Project
Run cargo clippy --all-targets --all-features to see:
- Performance suggestions
- Style improvements
- Correctness warnings
- Complexity issues
When to Allow Lints
- Performance: When the "better" way is actually slower
- Readability: When the lint makes code less readable
- API compatibility: When changing would break public API
- Test code: Some lints are less important in tests
- Unsafe code: Some patterns are necessary in unsafe blocks