#[warn(...)]
#[warn(lint_name)] (and its crate/module-level inner form #![warn(lint_name)]) configures the compiler to emit a non-fatal warning diagnostic whenever the specified lint condition is detected, while still allowing compilation to succeed.
Why #![warn(...)] Is Preferred Over #![deny(...)] in Source Code
Library authors and application teams commonly enable opt-in quality lints—which are allow by default in rustc—using #![warn(...)] in src/lib.rs (or [lints.rust] in Cargo.toml) rather than #![deny(...)]:
- Uninterrupted local iteration: During active development and debugging, temporary unused variables or missing doc comments on a scratch function emit visible warnings without blocking
cargo testfrom running. - Strict CI enforcement via
RUSTFLAGS="-D warnings": Continuous Integration (CI) pipelines can turn all warnings into hard errors during PR checks without hardcoding#![deny(...)]in source files. - Upstream compiler compatibility: If a new version of
rustcorclippyrefines a lint heuristic,#![warn(...)]never breaks downstream builds of your published crate on crates.io (cargoautomatically caps lints inside upstream dependencies anyway, but keeping#![warn]avoids breaking workspace builds).
High-Value Opt-In Compiler Lints (allow-by-default in rustc)
Several useful rustc lints are disabled (allow) by default and can be activated with #![warn(...)]:
Opt-In rustc Lint |
What It Warns About |
|---|---|
missing_docs |
Public items (pub fn, pub struct, pub trait) that lack a /// doc comment |
missing_debug_implementations |
Public types that do not implement std::fmt::Debug |
unreachable_pub |
Items marked pub inside a private module that are not re-exported publicly |
unused_qualifications |
Redundant module prefixes (such as writing std::option::Option::Some(1) instead of Some(1)) |
trivial_numeric_casts |
Unnecessary as casts where the source and destination types are already identical (42_u32 as u32) |
rust_2018_idioms |
Lint group encouraging explicit '_ anonymous lifetimes and modern module idioms |
#![warn(
missing_debug_implementations,
unreachable_pub,
unused_qualifications,
reason = "Enforce clean public API hygiene"
)]
/// A documented, `Debug`-implementing public configuration struct.
#[derive(Debug)]
pub struct EndpointConfig {
/// Port number for incoming requests.
pub port: u16,
}
fn main() {
let cfg = EndpointConfig { port: 9090 };
println!("configured: {:?}", cfg);
}
configured: EndpointConfig { port: 9090 }
Overriding an Inner #![allow(...)] with a Scoped #[warn(...)]
Because lint attributes stack lexically from outer scope to inner scope, placing #[warn(lint_name)] on a specific module or function re-enables warnings inside a file that otherwise had #![allow(lint_name)] at the top.