Skip to main content

allow

Attribute allow 

Source
Expand description

The allow attribute suppresses lint diagnostics that would otherwise produce warnings or errors. It can be used on any lint or lint group (except those set to forbid).

#[allow(dead_code)]
fn unused_function() {
    // ...
}

fn main() {
  // `unused_function` does not generate a compiler warning.
}

Without #[allow(dead_code)], the example above would emit:

warning: function `unused_function` is never used
 --> main.rs:1:4
  |
1 | fn unused_function() {
  |    ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

  warning: 1 warning emitted

Multiple lints can be set to allow at once with commas:

#[allow(unused_variables, unused_mut)]
fn main() {
    let mut x: u32 = 42;
}

This is mostly used to prevent lint warnings or errors while still under development.

It cannot override a lint that has been set to forbid.

It’s also important to consider that overusing allow could make code harder to maintain and possibly hide issues. To mitigate this issue, using the expect attribute is preferred.

allow can be overridden by warn, deny, and forbid.

The lint checks supported by rustc can be found via rustc -W help, along with their default settings and are documented in the rustc book.

For more information, see the Reference on the allow attribute.