By DeepSource

Unnecessary OnceCell wrapper around Mutex RS-W1135


When using static Mutex in Rust, it was common practice to wrap them inside a OnceCell for safe initialization. Since Rust 1.63, this is no longer necessary as global static Mutex's can be initialized without any wrapper. Therefore, using a OnceCell with a Mutex is redundant and adds unnecessary complexity to the code.

If your code uses a OnceCell to wrap a global static Mutex, you can simplify it by removing the OnceCell wrapper and initializing the Mutex directly.

Bad practice

use std::sync::Mutex;
use once_cell::sync::OnceCell;

static MY_MUTEX: OnceCell<Mutex<u32>> = OnceCell::with_value(Mutex::new(0));


use std::sync::Mutex;

static MY_MUTEX: Mutex<u32> = Mutex::new(0);