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(...)]).
#[allow(unused_variables)] fn demo() { let x = 1; }
Try it
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.
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
#[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. forbidcannot be lowered later in the same scope; an innerallowgives an error.- Command-line flags (
-A,-W,-D,-Finrustc;RUSTFLAGSor[lints]inCargo.toml) set the level for the whole crate, and attributes override them unlessforbidwas 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
expectoverallowfor temporary suppressions: when the underlying problem is fixed, the compiler tells you to delete the attribute. - Deny in CI. Many teams use
-D warningsin CI (not in development) so warnings cannot pile up, or set[lints.rust]and[lints.clippy]tables inCargo.toml. #![forbid(unsafe_code)]at the top of a crate is the standard way to promise a crate has nounsafe.- 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 plainrustcwithout error.