Esc

unpaired_lock

Finds a @Locks call with a path that returns or ends without a matching @Unlocks.

Category suspicious, warn by default.

Example
#import "Basic";
#import "Thread";

State :: struct {
    mutex: Mutex;
    count: int;
    closed: bool;
}

bump :: (state: *State) -> bool {
    lock(*state.mutex);
    if state.closed return false;

    state.count += 1;
    unlock(*state.mutex);

    return true;
}

main :: () {
    state: State;
    init(*state.mutex);
    print("%\n", bump(*state));
}
Diagnostic
warning[unpaired_lock]: this `return` leaves `*state.mutex` locked
  --> example.jai:12:21
   |
12 |     if state.closed return false;
   |                     ^^^^^^^^^^^^^
   |
   = help: unlock it before returning, or `defer` the unlock right after locking
   = note: `unpaired_lock` is `warn` by default
Fixed
#import "Basic";
#import "Thread";

State :: struct {
    mutex: Mutex;
    count: int;
    closed: bool;
}

bump :: (state: *State) -> bool {
    lock(*state.mutex);
    defer unlock(*state.mutex);

    if state.closed return false;

    state.count += 1;

    return true;
}

main :: () {
    state: State;
    init(*state.mutex);
    print("%\n", bump(*state));
}

A statement-level call of a @Locks procedure with an argument (lock(*state.mutex);) at the top level of a procedure body, followed by a path that reaches a.