<< All versions
Skill v1.0.0
currentAutomated scan100/100willyd61/nautobot-v2-custom-metrics-old/rust-quality
──Details
PublishedNovember 22, 2026 at 05:13 PM
Content Hashsha256:f6832a3822b034cf...
Git SHA1cada91011ea
──Files
Files (1 file, 10.9 KB)
SKILL.md10.9 KBactive
SKILL.md · 529 lines · 10.9 KB
name: rust-quality description: | Rust code quality with Clippy, rustfmt, and cargo tools. Covers linting, formatting, and idiomatic Rust patterns.
USE WHEN: user works with "Rust", "Cargo", "Actix", "Axum", asks about "Clippy", "rustfmt", "Rust linting", "Rust best practices", "idiomatic Rust"
DO NOT USE FOR: SonarQube - use sonarqube skill, security - use rust-security skill allowed-tools: Read, Grep, Glob, Bash metadata: version: "1.0.0"
Rust Quality - Quick Reference
When NOT to Use This Skill
- SonarQube setup - Use
sonarqubeskill - Security scanning - Use
rust-securityskill
Deep Knowledge: Usemcp__documentation__fetch_docswith technology:rustfor comprehensive documentation.
Tool Overview
| Tool | Focus | Command | |
|---|---|---|---|
| rustfmt | Formatting | cargo fmt | |
| Clippy | Linting | cargo clippy | |
| rust-analyzer | IDE analysis | LSP | |
| cargo-deny | Dependency policy | cargo deny | |
| cargo-audit | Security audit | cargo audit |
Clippy Setup
Run Clippy
bash
# Basic checkcargo clippy# Strict mode - deny all warningscargo clippy -- -D warnings# With pedantic lintscargo clippy -- -W clippy::pedantic# Fix auto-fixablecargo clippy --fix# All targets (tests, benches, examples)cargo clippy --all-targets --all-features
clippy.toml
toml
# Maximum cognitive complexitycognitive-complexity-threshold = 15# Maximum function lengthtoo-many-lines-threshold = 50# Maximum argumentstoo-many-arguments-threshold = 5# Allowed wildcard importsallowed-wildcard-imports = ["crate::prelude::*"]
Cargo.toml Lint Configuration
toml
[lints.rust]unsafe_code = "deny"missing_docs = "warn"[lints.clippy]# Categoriespedantic = { level = "warn", priority = -1 }nursery = { level = "warn", priority = -1 }# Specific lintsunwrap_used = "warn"expect_used = "warn"panic = "warn"todo = "warn"dbg_macro = "warn"# Allow specific pedantic lintsmodule_name_repetitions = "allow"must_use_candidate = "allow"
CI Configuration
bash
# Strict CI checkcargo clippy --all-targets --all-features -- \-D warnings \-D clippy::pedantic \-D clippy::nursery \-A clippy::module_name_repetitions
rustfmt Setup
rustfmt.toml
toml
edition = "2021"max_width = 100tab_spaces = 4newline_style = "Unix"# Importsimports_granularity = "Module"group_imports = "StdExternalCrate"reorder_imports = true# Itemsreorder_modules = truereorder_impl_items = true# Formattinguse_small_heuristics = "Default"fn_single_line = falsewhere_single_line = falsestruct_lit_single_line = true# Commentscomment_width = 100wrap_comments = truenormalize_comments = true# Macrosformat_macro_matchers = trueformat_macro_bodies = true
Commands
bash
# Format allcargo fmt# Check without changingcargo fmt -- --check# Format specific filerustfmt src/main.rs
Common Clippy Lints
unwrap_used / expect_used
rust
// BAD - Panics on None/Errlet value = some_option.unwrap();let result = some_result.expect("should work");// GOOD - Handle errors properlylet value = some_option.ok_or(MyError::NotFound)?;let result = some_result.map_err(|e| MyError::from(e))?;// GOOD - When panic is intentional (with justification)let value = config.get("required_key").expect("required_key must be set in configuration");
clone_on_ref_ptr
rust
// BAD - Cloning Arc/Rc unnecessarilylet clone = arc_value.clone();// GOOD - Use Arc::clone for claritylet clone = Arc::clone(&arc_value);
needless_pass_by_value
rust
// BAD - Takes ownership unnecessarilyfn process(data: String) {println!("{}", data);}// GOOD - Borrow insteadfn process(data: &str) {println!("{}", data);}
cognitive_complexity
rust
// BAD - Too complexfn process(data: &Data) -> Result<Output, Error> {if data.is_valid() {if data.type_a() {if data.has_value() {// deep nesting...}}}// ... more conditions}// GOOD - Extract and simplifyfn process(data: &Data) -> Result<Output, Error> {validate(data)?;match data.data_type() {DataType::A => process_type_a(data),DataType::B => process_type_b(data),}}
missing_errors_doc
rust
// BAD - No error documentation/// Processes the data.pub fn process(data: &Data) -> Result<Output, Error> { ... }// GOOD - Document errors/// Processes the data.////// # Errors////// Returns `Error::InvalidData` if the data is malformed./// Returns `Error::NotFound` if the resource doesn't exist.pub fn process(data: &Data) -> Result<Output, Error> { ... }
Common Code Smells & Fixes
1. Stringly Typed Code
rust
// BAD - Using strings for typesfn set_status(status: &str) {match status {"active" => { ... }"inactive" => { ... }_ => panic!("unknown status"),}}// GOOD - Use enums#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Status {Active,Inactive,}fn set_status(status: Status) {match status {Status::Active => { ... }Status::Inactive => { ... }}}
2. Error Handling
rust
// BAD - Using unwrap in library codepub fn parse_config(path: &Path) -> Config {let content = fs::read_to_string(path).unwrap();serde_json::from_str(&content).unwrap()}// GOOD - Proper error handling with thiserror#[derive(Debug, thiserror::Error)]pub enum ConfigError {#[error("failed to read config file: {0}")]Io(#[from] std::io::Error),#[error("failed to parse config: {0}")]Parse(#[from] serde_json::Error),}pub fn parse_config(path: &Path) -> Result<Config, ConfigError> {let content = fs::read_to_string(path)?;let config = serde_json::from_str(&content)?;Ok(config)}
3. Builder Pattern
rust
// BAD - Constructor with many parametersimpl Server {pub fn new(host: String,port: u16,max_connections: usize,timeout: Duration,tls_config: Option<TlsConfig>,) -> Self { ... }}// GOOD - Builder pattern#[derive(Default)]pub struct ServerBuilder {host: String,port: u16,max_connections: usize,timeout: Duration,tls_config: Option<TlsConfig>,}impl ServerBuilder {pub fn host(mut self, host: impl Into<String>) -> Self {self.host = host.into();self}pub fn port(mut self, port: u16) -> Self {self.port = port;self}pub fn build(self) -> Result<Server, BuildError> {// Validate and build}}// Usagelet server = Server::builder().host("localhost").port(8080).build()?;
4. Newtype Pattern
rust
// BAD - Primitive obsessionfn create_user(email: String, name: String, age: u32) { ... }// GOOD - Newtypes for validation#[derive(Debug, Clone)]pub struct Email(String);impl Email {pub fn new(value: impl Into<String>) -> Result<Self, ValidationError> {let value = value.into();if !value.contains('@') {return Err(ValidationError::InvalidEmail);}Ok(Self(value))}pub fn as_str(&self) -> &str {&self.0}}fn create_user(email: Email, name: Name, age: Age) { ... }
5. Avoid clone() Abuse
rust
// BAD - Cloning everywherefn process(data: &Vec<Item>) {let owned = data.clone();for item in owned {// process}}// GOOD - Borrow when possiblefn process(data: &[Item]) {for item in data {// process}}// GOOD - Take ownership when neededfn process(data: Vec<Item>) {for item in data {// consume item}}
Pre-commit Setup
.pre-commit-config.yaml
yaml
repos:- repo: localhooks:- id: cargo-fmtname: cargo fmtentry: cargo fmt --language: systemtypes: [rust]- id: cargo-clippyname: cargo clippyentry: cargo clippy --all-targets --all-features -- -D warningslanguage: systemtypes: [rust]pass_filenames: false- id: cargo-testname: cargo testentry: cargo testlanguage: systemtypes: [rust]pass_filenames: false
Makefile
makefile
.PHONY: fmt lint test check qualityfmt:cargo fmtlint:cargo clippy --all-targets --all-features -- -D warningstest:cargo testcheck:cargo check --all-targets --all-featuresquality: fmt check lint test
VS Code Settings
json
// .vscode/settings.json{"[rust]": {"editor.defaultFormatter": "rust-lang.rust-analyzer","editor.formatOnSave": true},"rust-analyzer.check.command": "clippy","rust-analyzer.check.extraArgs": ["--all-targets", "--all-features"],"rust-analyzer.diagnostics.disabled": [],"rust-analyzer.lens.run.enable": true,"rust-analyzer.lens.debug.enable": true}
Quality Metrics Targets
| Metric | Target | Tool | |
|---|---|---|---|
| Cognitive Complexity | < 15 | Clippy | |
| Function Lines | < 50 | Clippy | |
| Arguments | < 5 | Clippy | |
| unsafe blocks | Minimize | Clippy | |
| Test Coverage | > 80% | cargo-tarpaulin |
CI/CD Integration
GitHub Actions
yaml
name: Qualityon: [push, pull_request]jobs:quality:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v4- name: Install Rustuses: dtolnay/rust-toolchain@stablewith:components: rustfmt, clippy- name: Cache cargouses: Swatinem/rust-cache@v2- name: Check formattingrun: cargo fmt -- --check- name: Clippyrun: cargo clippy --all-targets --all-features -- -D warnings- name: Run testsrun: cargo test --all-features- name: Build docsrun: cargo doc --no-depsenv:RUSTDOCFLAGS: -D warnings
Anti-Patterns
| Anti-Pattern | Why It's Bad | Correct Approach | |
|---|---|---|---|
unwrap() in library code | Panics propagate | Use ? operator | |
Excessive clone() | Performance cost | Borrow when possible | |
#[allow(clippy::all)] | Hides all issues | Allow specific lints | |
unsafe without comment | Unclear safety | Document invariants | |
| String for everything | No type safety | Use enums/newtypes | |
| Giant functions | Hard to test/maintain | Extract smaller functions |
Quick Troubleshooting
| Issue | Likely Cause | Solution | |
|---|---|---|---|
| Clippy false positive | Edge case or intended | #[allow(clippy::lint)] with comment | |
| rustfmt changes code | Formatting opinion | Configure rustfmt.toml | |
| Lint conflicts | Pedantic vs nursery | Prioritize in Cargo.toml | |
| Slow compilation | Many dependencies | Use cargo-chef for caching | |
| Dead code warnings | Unused exports | Add #[cfg(test)] or remove |