Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Attributes

The lint attributes allow, warn, deny and expect

Reference for Rust lint attributes: allow, warn, deny, forbid and expect, how scopes and overrides work, the reason field, and command-line equivalents.

A lint is a check that reports suspicious or stylistic problems without stopping a correct program from compiling. Every lint has a level, and the lint attributes change that level for the item they are attached to: allow silences it, warn reports a warning, deny turns it into an error, forbid is a deny that cannot be overridden later, and expect silences it but warns if the lint turns out not to fire.

Attributes apply to the item that follows (#[allow(...)]) or, in the inner form, to the whole enclosing module or crate (#![allow(...)]).

Rust
#[allow(unused_variables)]
fn demo() {
    let x = 1;
}

Try it

Rust Silencing and escalating lints

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 expect and reason

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, other_lint)]
#[warn(lint_name)]
#[deny(lint_name)]
#[forbid(lint_name)]
#[expect(lint_name, reason = "why")]
#![allow(lint_group)]               // inner form
Level Behavior
allow Lint is silent
warn Reports a warning; the build still succeeds
deny Reports an error; the build fails
forbid Like deny, and inner allow attempts are rejected
expect Silences the lint, and warns (unfulfilled_lint_expectations) if it never fires

Every attribute accepts several lint names, tool-scoped names such as clippy::pedantic or rustdoc::broken_intra_doc_links, and groups like warnings, unused, rust_2018_idioms. The optional reason = "..." string (stable since Rust 1.81) is shown in diagnostics.

Errors and edge cases:

  • An unknown lint name is reported as unknown_lints.
  • forbid cannot be lowered later in the same scope; an inner allow gives an error.
  • Command-line flags (-A, -W, -D, -F in rustc; RUSTFLAGS or [lints] in Cargo.toml) set the level for the whole crate, and attributes override them unless forbid was used.

Usage notes

  • Scope narrowly. Put #[allow] on the smallest item that needs it, not on a whole crate, so new problems still show up.
  • Prefer expect over allow for temporary suppressions: when the underlying problem is fixed, the compiler tells you to delete the attribute.
  • Deny in CI. Many teams use -D warnings in CI (not in development) so warnings cannot pile up, or set [lints.rust] and [lints.clippy] tables in Cargo.toml.
  • #![forbid(unsafe_code)] at the top of a crate is the standard way to promise a crate has no unsafe.
  • Order inside a scope. Later attributes on the same item override earlier ones for the same lint, except through forbid.
  • Clippy lints only run under cargo clippy, but the attributes naming them are accepted by plain rustc without error.