| created | "2025-12-16T00:00:00.000Z" |
| modified | "2025-12-16T00:00:00.000Z" |
| reviewed | "2025-12-16T00:00:00.000Z" |
| name | clippy-advanced |
| description | Advanced Clippy configuration for comprehensive Rust linting with custom rules, categories, and IDE integration.
Use when configuring linting rules, enforcing code standards, setting up CI linting, or customizing clippy behavior.
Trigger terms: clippy, linting, code quality, clippy.toml, pedantic, nursery, restriction, lint configuration, code standards.
|
clippy-advanced - Advanced Clippy Configuration
Advanced Clippy configuration for comprehensive Rust linting, including custom rules, lint categories, disallowed methods, and IDE integration.
Installation
rustup component add clippy
cargo clippy --version
rustup update
Basic Usage
cargo clippy
cargo clippy --all-targets
cargo clippy --all-features
cargo clippy --workspace --all-targets --all-features
cargo clippy -- -W clippy::all -A clippy::pedantic
cargo clippy -- -D warnings
clippy.toml Configuration File
Create clippy.toml or .clippy.toml in project root:
missing-docs-in-crate-items = "warn"
cognitive-complexity-threshold = 15
type-complexity-threshold = 100
too-many-lines-threshold = 100
too-many-arguments-threshold = 5
vec-box-size-threshold = 4096
disallowed-methods = [
{ path = "std::env::var", reason = "Use std::env::var_os for better Unicode handling" },
{ path = "std::panic::catch_unwind", reason = "Prefer structured error handling" },
{ path = "std::process::exit", reason = "Use Result propagation instead" },
]
disallowed-types = [
{ path = "std::collections::HashMap", reason = "Use indexmap::IndexMap for deterministic iteration" },
{ path = "once_cell::sync::Lazy", reason = "Use std::sync::LazyLock (Rust 1.80+)" },
]
allowed-idents-below-min-chars = ["i", "j", "x", "y", "id", "db"]
imports-granularity = "module"
allow-module-inception = false
imports-prefer-module = true
single-component-path-imports = "warn"
array-size-threshold = 512
literal-representation = "always"
allowed-confusable-scripts = ["Latin", "Greek"]
semicolon-if-nothing-returned = "always"
disallowed-names = ["foo", "bar", "baz", "master", "slave", "whitelist", "blacklist"]
doc-markdown-inline-code = true
Lint Categories
All Available Categories
cargo clippy -- -W clippy::all
cargo clippy -- -W clippy::pedantic
cargo clippy -- -W clippy::restriction
cargo clippy -- -W clippy::nursery
cargo clippy -- -W clippy::cargo
cargo clippy -- -W clippy::complexity
cargo clippy -- -W clippy::correctness
cargo clippy -- -W clippy::perf
cargo clippy -- -W clippy::style
cargo clippy -- -W clippy::suspicious
Recommended Category Combination
[workspace.lints.clippy]
correctness = "deny"
complexity = "warn"
perf = "warn"
style = "warn"
suspicious = "warn"
pedantic = "warn"
must_use_candidate = "allow"
missing_errors_doc = "allow"
clone_on_ref_ptr = "warn"
dbg_macro = "warn"
print_stdout = "warn"
todo = "warn"
unimplemented = "warn"
use_self = "warn"
Cargo.toml Lint Configuration (Recommended)
[workspace.lints.clippy]
correctness = { level = "deny", priority = -1 }
complexity = "warn"
cognitive_complexity = "warn"
too_many_arguments = "warn"
too_many_lines = "warn"
type_complexity = "warn"
perf = "warn"
large_enum_variant = "warn"
large_stack_arrays = "warn"
style = "warn"
missing_docs_in_private_items = "warn"
pedantic = "warn"
must_use_candidate = "allow"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
module_name_repetitions = "allow"
clone_on_ref_ptr = "warn"
dbg_macro = "warn"
empty_drop = "warn"
exit = "warn"
expect_used = "warn"
filetype_is_file = "warn"
get_unwrap = "warn"
panic = "warn"
print_stderr = "warn"
print_stdout = "warn"
todo = "warn"
unimplemented = "warn"
unreachable = "warn"
unwrap_used = "warn"
use_self = "warn"
useless_let_if_seq = "warn"
cargo = "warn"
multiple_crate_versions = "warn"
[workspace.lints.rust]
missing_docs = "warn"
unsafe_code = "warn"
Allow and Deny Attributes
Function-level Overrides
#[allow(clippy::too_many_arguments)]
fn complex_function(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) {
}
#[deny(clippy::unwrap_used)]
fn critical_function() -> Result<(), Error> {
Ok(())
}
#[warn(clippy::print_stdout)]
fn debug_function() {
println!("This will warn");
}
Module-level Configuration
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::must_use_candidate)]
#[allow(clippy::pedantic)]
mod legacy_code {
}
Inline Suppression
#[allow(clippy::cast_possible_truncation)]
let x = value as u8;
let y = {
#[allow(clippy::cast_sign_loss)]
negative_value as u32
};
{
#![allow(clippy::indexing_slicing)]
let element = slice[index];
}
CI Integration
GitHub Actions - Strict Linting
name: Clippy
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- name: Run clippy
run: |
cargo clippy --workspace --all-targets --all-features -- -D warnings
- name: Run clippy pedantic
run: |
cargo clippy --workspace --all-targets --all-features -- \
-W clippy::pedantic \
-W clippy::nursery \
-D clippy::correctness
GitHub Actions - Reviewdog Integration
name: Clippy Review
on: [pull_request]
jobs:
clippy-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- uses: giraffate/clippy-action@v1
with:
reporter: 'github-pr-review'
github_token: ${{ secrets.GITHUB_TOKEN }}
clippy_flags: --all-targets --all-features
Pre-commit Hook
repos:
- repo: local
hooks:
- id: cargo-clippy
name: cargo clippy
entry: cargo clippy
args: ['--all-targets', '--all-features', '--', '-D', 'warnings']
language: system
pass_filenames: false
files: \.rs$
rust-analyzer Integration
Configure in VS Code settings or rust-analyzer config:
{
"rust-analyzer.check.command": "clippy",
"rust-analyzer.check.extraArgs": [
"--all-targets",
"--all-features",
"--",
"-W",
"clippy::pedantic",
"-W",
"clippy::nursery"
],
"rust-analyzer.checkOnSave": true
}
Or in rust-analyzer.toml:
[checkOnSave]
command = "clippy"
extraArgs = [
"--all-targets",
"--all-features",
"--",
"-W", "clippy::pedantic",
"-W", "clippy::nursery",
"-A", "clippy::module_name_repetitions"
]
Disallowed Methods Configuration
disallowed-methods = [
{ path = "std::option::Option::unwrap", reason = "Use unwrap_or, unwrap_or_else, or proper error handling" },
{ path = "std::result::Result::unwrap", reason = "Use unwrap_or, unwrap_or_else, or the ? operator" },
{ path = "std::option::Option::expect", reason = "Use unwrap_or, unwrap_or_else, or proper error handling" },
{ path = "std::result::Result::expect", reason = "Use unwrap_or, unwrap_or_else, or the ? operator" },
{ path = "std::panic", reason = "Use Result and proper error handling" },
{ path = "std::process::exit", reason = "Return from main or propagate errors" },
{ path = "std::env::var", reason = "Use std::env::var_os for better Unicode handling" },
{ path = "std::println", reason = "Use logging (log, tracing) instead of println" },
{ path = "std::eprintln", reason = "Use logging (log, tracing) instead of eprintln" },
]
disallowed-types = [
{ path = "std::sync::mpsc::channel", reason = "Use crossbeam-channel for better performance" },
{ path = "std::collections::HashMap", reason = "Consider indexmap for deterministic ordering" },
{ path = "std::mem::uninitialized", reason = "Use MaybeUninit instead" },
]
Advanced Patterns
Conditional Linting for Tests
#![deny(clippy::unwrap_used)]
#![allow(clippy::unwrap_used)]
#[cfg(test)]
mod tests {
#[test]
fn test_something() {
let value = Some(42);
assert_eq!(value.unwrap(), 42);
}
}
Feature-gated Lints
#[cfg(not(feature = "unsafe_optimizations"))]
#![deny(unsafe_code)]
#[cfg(feature = "unsafe_optimizations")]
#![warn(unsafe_code)]
Documentation Lints
#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]
pub fn public_function() {
}
fn private_function() {
}
Best Practices
-
Start with defaults, gradually enable stricter lints:
[workspace.lints.clippy]
all = "warn"
correctness = "deny"
-
Use Cargo.toml for workspace-wide configuration:
[workspace.lints.clippy]
-
Document why lints are suppressed:
#[allow(clippy::cast_possible_truncation)]
let x = value as u8;
-
Treat warnings as errors in CI:
- run: cargo clippy -- -D warnings
-
Enable pedantic selectively:
pedantic = "warn"
must_use_candidate = "allow"
-
Use disallowed-methods for project-specific rules:
disallowed-methods = [
{ path = "std::println", reason = "Use tracing instead" }
]
-
Integrate with rust-analyzer for real-time feedback
-
Review and update configuration as project matures
Troubleshooting
Too many warnings:
cargo clippy -- -W clippy::correctness
False positives:
#[allow(clippy::specific_lint)]
fn special_case() { }
Lint not applying:
cargo clippy --version
rustup update
CI failures due to new lints:
[toolchain]
channel = "1.75.0"
components = ["clippy"]
References