> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Forbid Attribute

The `forbid` attribute is a compiler directive in Rust that sets the severity level of a specified lint or lint group to a fatal error while simultaneously preventing that level from being overridden. It is the strictest lint level available in the Rust compiler (`rustc`). Unlike the `deny` attribute, which also turns lints into errors, `forbid` establishes an immutable state for the lint within the lexical scope and item hierarchy; once applied, the specified lint cannot be downgraded in any nested scopes.

## Syntax

The attribute can be applied as an inner attribute (affecting the entire enclosing crate or module) or an outer attribute (affecting a specific item, statement, or expression, and its descendants). Multiple lints can be passed as a comma-separated list.

```rust theme={"dark"}
// Inner attribute syntax (typically placed at the top of lib.rs or main.rs)
#![forbid(lint_name, another_lint_name)]

// Outer attribute syntax (placed above a function, struct, module, statement, or expression)
#[forbid(lint_group_name)]
fn example_function() {}
```

## Manifest Configuration (Cargo.toml)

As of Rust 1.74, the idiomatic method for configuring global lint levels is through the `[lints]` table in the `Cargo.toml` manifest. This applies the `forbid` level across the entire crate without requiring source code attributes or command-line flags.

```toml theme={"dark"}
[lints.rust]
unused_variables = "forbid"

[lints.clippy]
all = "forbid"
```

## Command-Line Configuration

The `forbid` level can also be applied globally during compilation using the command-line interface. This is achieved by passing the `-F` or `--forbid` flag directly to `rustc` or through Cargo.

```bash theme={"dark"}

# Using rustc directly
rustc -F unused_variables main.rs


# Using Cargo with a built-in compiler lint
cargo rustc -- -F unused_variables


# Using Cargo to forbid Clippy lints (requires the clippy driver)
cargo clippy -- -F clippy::all
```

## Compiler Behavior and Mechanics

When `rustc` processes a `forbid` directive, it enforces two strict rules:

1. **Compilation Failure:** Any code violating the forbidden lint will emit a compiler error, ensuring the build ultimately fails. The compiler does not halt immediately upon encountering the violation; it continues analyzing the code to report as many errors as possible in the current compilation phase before terminating.
2. **Override Rejection:** Any attempt to redefine the lint level using `#[allow(...)]`, `#[warn(...)]`, or `#[deny(...)]` further down the item hierarchy will trigger an `E0453` compiler error ("allow/warn/deny overruled by outer forbid").

## Mechanical Demonstration

The following code illustrates the mechanical difference between a standard lint violation and an illegal override attempt under a `forbid` directive:

```rust theme={"dark"}
// Enforce the forbid level for the entire module/crate
#![forbid(unused_variables)]

fn standard_violation() {
    // ERROR: unused variable: `x`
    // The compiler flags this as an error and will fail the build,
    // but continues checking the rest of the file.
    let x = 5; 
}

fn override_attempt() {
    // ERROR[E0453]: allow(unused_variables) overruled by outer forbid
    // The compiler rejects the attribute itself, regardless of whether 
    // a variable is actually unused.
    #[allow(unused_variables)]
    let y = 10;
}
```

## Interaction with Lint Groups

When `forbid` is applied to a lint group (e.g., `rust_2018_idioms` or `clippy::all`), the immutability applies to every individual lint contained within that group. Attempting to `allow` a specific, single lint that belongs to a forbidden group will result in the same `E0453` override error.

```rust theme={"dark"}
#[forbid(nonstandard_style)]
mod example_module {
    // ERROR[E0453]: allow(non_camel_case_types) overruled by outer forbid
    // `non_camel_case_types` is a constituent of the `nonstandard_style` group.
    #[allow(non_camel_case_types)]
    struct my_struct;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
