CONST_ITEM_INTERIOR_MUTATIONS in rustc_lint::interior_mutable_consts - Rust
Description: The `const_item_interior_mutations` lint checks for calls which mutates an interior mutable const-item.

Skip to main content

## CONST_ITEM_INTERIOR_MUTATIONS

# Static CONST_ITEM_INTERIOR_MUTATIONS

Source

    pub static CONST_ITEM_INTERIOR_MUTATIONS: &Lint

Expand description

The `const_item_interior_mutations` lint checks for calls which mutates an interior mutable const-item.

#### §Example

    use std::sync::Once;

    const INIT: Once = Once::new(); // using `INIT` will always create a temporary and
                                    // never modify it-self on use, should be a `static`
                                    // instead for shared use

    fn init() {
        INIT.call_once(|| {
            println!("Once::call_once first call");
        });
        INIT.call_once(|| {                          // this second will also print
            println!("Once::call_once second call"); // as each call to `INIT` creates
        });                                          // new temporary
    }

{{produces}}

#### §Explanation

Calling a method which mutates an interior mutable type has no effect as const-item are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used rendering modification through interior mutability ineffective across usage of that const-item.

The current implementation of this lint only warns on significant `std` and `core` interior mutable types, like `Once`, `AtomicI32`, … this is done out of prudence to avoid false-positive and may be extended in the future.