Rust Tooling and Code Quality

Addressing Clippy Warnings

Addressing Clippy warnings systematically improves a Rust codebase's memory efficiency, readability, and API ergonomics by replacing common anti-patterns—such as overly concrete reference parameters, eager option defaults, and manual iterator loops—with idiomatic zero-cost constructs.

High-Impact Clippy Warnings and Their Idiomatic Fixes

Clippy Lint Non-Idiomatic Code Idiomatic Replacement Why It Matters
clippy::ptr_arg fn f(s: &String) or fn f(v: &Vec<T>) fn f(s: &str) or fn f(v: &[T]) Avoids double indirection and allows callers to pass string/array literals or sub-slices without allocating.
clippy::or_fun_call opt.unwrap_or(compute()) opt.unwrap_or_else(|| compute()) Prevents evaluating compute() eagerly when opt is already Some(val).
clippy::needless_pass_by_value fn f(cfg: String) (when only read) fn f(cfg: &str) Eliminates an unnecessary heap clone at the call site.
clippy::large_enum_variant enum E { Small(u8), Huge([u8; 4096]) } enum E { Small(u8), Huge(Box<[u8; 4096]>) } Prevents every instance of E from occupying 4 KiB on the stack.
clippy::map_entry if !map.contains_key(&k) { map.insert(k, v); } map.entry(k).or_insert(v); Performs a single hash lookup instead of hashing and probing the table twice.
clippy::redundant_clone let y = x.clone(); (when x is never used again) let y = x; (move directly) Removes an unnecessary heap allocation or reference-count bump.

Refactoring Before and After Clippy Suggestions

use std::collections::HashMap;

// BEFORE: Triggers `clippy::ptr_arg`, `clippy::map_entry`, and `clippy::or_fun_call`
// fn record_visit_bad(counts: &mut HashMap<String, u32>, host: &String) -> u32 { ... }

// AFTER: Idiomatic, zero-warning implementation
pub fn record_visit(counts: &mut HashMap<String, u32>, host: &str) -> u32 {
    let counter = counts
        .entry(host.to_string())
        .or_insert_with(|| 0);
    *counter += 1;
    *counter
}

pub fn resolve_endpoint<'a>(custom: Option<&'a str>, default_port: u16) -> String {
    // Lazy fallback via `map_or_else` avoids formatting the default string when `custom` is `Some`
    custom.map_or_else(
        || format!("http://127.0.0.1:{default_port}"),
        str::to_string,
    )
}

fn main() {
    let mut visits = HashMap::new();
    println!("Visit #1: {}", record_visit(&mut visits, "docs.rs"));
    println!("Visit #2: {}", record_visit(&mut visits, "docs.rs"));
    println!("Endpoint: {}", resolve_endpoint(Some("https://crates.io"), 8080));
}
Visit #1: 1
Visit #2: 2
Endpoint: https://crates.io

Scoped Suppression with #[expect(..., reason = "...")]

Sometimes an intentional design decision (for example, an FFI callback signature that must take a specific pointer type, or a math formula where 1 * x aligns a matrix) triggers a stylistic Clippy lint.

Rather than disabling the lint across the entire crate, attach #[expect(clippy::lint_name, reason = "...")] to the narrowest possible item or expression:

#[expect(
    clippy::identity_op,
    reason = "1 * DAY_SECS documents the 1-day multiplier alongside multi-day constants"
)]
pub const ONE_DAY_SECS: u64 = 1 * 24 * 60 * 60;
pub const SEVEN_DAYS_SECS: u64 = 7 * 24 * 60 * 60;

fn main() {
    println!("1 day = {ONE_DAY_SECS}s, 7 days = {SEVEN_DAYS_SECS}s");
}
1 day = 86400s, 7 days = 604800s