Skip to content

T0002 lock-unwrap

Rule: A crate keeps its lock().unwrap() calls under a limit.

Code T0002 (stable, never reused)
Id lock-unwrap
Category state
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small off
small-workspace off
medium warn the count of lock().unwrap() per crate shows where shared state concentrates
big warn the count of lock().unwrap() per crate shows where shared state concentrates
ai off
embedded off
ui off
egui off
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
max integer 0 calls allowed per crate before it is reported

Why

Each .lock().unwrap() is a lock reached from more than one place, and a panic when another thread panicked while holding it (poison). In tpm's crates there were about 1,200 of them. A count per crate shows where shared state concentrates and whether a refactor is making it shrink.

The lint counts .lock(), .read() and .write() with no arguments followed by .unwrap() or .expect(..), outside test code, and reports a crate whose count passes max, with the first places.

How to fix

Give the state one owner (see static-interior-mutability). Locks that stay are taken through one small helper that decides what poison means (recover with into_inner, or fail with a message), so the count goes to zero.

Configure

[lints.lock-unwrap]
level = "deny"
max = 0
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(lock-unwrap) <reason> (waivers); the code works there too, as in [lints.T0002].