Skip to content

Rust Reference ยท Attributes

Common Rust lints and lint attributes

Reference for common rustc lints and the allow, warn, deny, forbid and expect attributes: dead_code, unused_variables, unused_must_use, unsafe_code and more.

A lint is a check the compiler runs on valid code to flag suspicious or unwanted patterns. Each lint has a name (dead_code, unused_variables) and a level that decides what happens when it fires. You change levels with attributes (#[allow(...)], #![deny(...)]) at item, module or crate scope, or with -A, -W, -D and -F flags on the command line.

Lints are about code quality, not language rules. A warning never stops a build, while a deny turns the same finding into an error.

Try it

Rust Levels at work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust deny turns a warning into an error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
#[allow(lint_name)]                      // item scope
#[warn(lint_a, lint_b)]
#![deny(lint_name, reason = "why")]      // inner attribute: whole crate or module
#[expect(lint_name)]
#[forbid(lint_name)]
Level Behavior
allow Lint is silent.
expect Silent, but emits unfulfilled_lint_expectations if the lint never fires (stable since Rust 1.81).
warn Prints a warning; build continues.
deny Reports an error; an inner allow can still override it.
forbid Like deny, but nested allow or warn is rejected.

Lint groups set many lints at once: unused (all unused_* lints plus dead_code), rust_2018_idioms, future_incompatible, nonstandard_style and warnings (every warning). Clippy adds its own groups under clippy:: and is read-only on this site.

Common lints

Lint Default Fires when
dead_code warn A function, field, variant or constant is never used.
unused_variables warn A local or parameter is never read.
unused_imports warn A use is never needed.
unused_mut warn A mut binding is never mutated.
unused_must_use warn A Result, #[must_use] value or future is dropped.
unused_assignments warn A value is assigned and overwritten before being read.
non_snake_case, non_camel_case_types, non_upper_case_globals warn Names break Rust conventions.
unreachable_code warn Code follows return, panic! or an endless loop.
unsafe_code allow Any unsafe use (turn on with #![forbid(unsafe_code)]).
missing_docs allow A public item has no doc comment.
arithmetic_overflow deny A constant expression provably overflows.
overflowing_literals deny A literal does not fit its type (let x: u8 = 256;).
unconditional_recursion warn A function always calls itself.
deprecated warn A #[deprecated] item is used.

Usage notes

  • Fix the cause before silencing a lint. When suppression is right, narrow it to one item and add reason = "..." (stable since 1.81).
  • Prefer #[expect] over #[allow] for temporary exceptions: it tells you when the exception is no longer needed.
  • #![forbid(unsafe_code)] at the crate root is a common way to promise a pure safe-Rust crate.
  • #![deny(warnings)] in library source breaks builds whenever a newer compiler adds a lint; set it in CI with RUSTFLAGS="-D warnings" instead.
  • Prefix a name with _ to mute unused_variables and dead_code for that name; a bare _ pattern drops the value immediately.
  • Command-line flags apply to the whole crate, attributes override them for their scope, and forbid beats later attributes.
  • cargo clippy and cargo fix are tools you run locally; their output is not available in the browser runner.