Skip to content

unbalanced-update ​

A change and the change that undoes it, written as a pair in one function (a flag set and cleared, a count taken and given back, an entry added and removed, a value changed and given back what it held), and a way out between the two that leaves with the first and without the second: an await that rejects, a call that throws, a return, a q(...) that returns the error, a throw, and in a loop's body a continue or a break. On by default, as a warning.

Why: the pair says the function means to bracket its work, and a way out that skips the undo leaves the world as nobody meant it: a busy flag stuck on (the button stays disabled for good), a count that never returns to zero (whoever waits on it waits for ever), an id that stays in the set of pending ones, a lock nobody drops. The path that forgets is the one you rarely run: the request that fails, the early return added later. Rust's answer is a guard whose Drop undoes the change on every way out; in TypeScript it is try / finally, or no hand-written flag at all.

✅ Accepted: in a component, useTask holds the task's state itself (Idle, Loading, Ready, Failed), so there is no flag to forget

✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { onMounted } from "vue";
import { Match } from "@evobug_com/safe-ts-vue/match";
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { fetchOrders } from "./api";

const { state, run } = useTask((signal) => fetchOrders(signal));

onMounted(() => {
  run().catch((reason: unknown) => console.error(reason));
});
</script>

<template>
  <Match :value="state">
    <template #Loading>Loading…</template>
    <template #Failed="{ error }">{{ error.message }}</template>
    <template #Ready="{ value }">
      <p v-for="order in value" :key="order.id">{{ order.total }}</p>
    </template>
    <template #_></template>
  </Match>
</template>

Elsewhere (a store, a class, a server's state) a flag by hand is fine when it is cleared on every way out.

❌ Rejected: if load() rejects, busy stays true

✕ Rejectedunbalanced-update
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
import { PropagateError } from "@evobug_com/safe-ts-std/result";

class Offline extends PropagateError {}

async function load(): Promise<string> {
  await Promise.resolve();
  if (Date.now() < 0) throw new Offline("offline");
  return "text";
}

interface Panel {
  busy: boolean;
  text: string;
}

export async function reload(panel: Mut<Panel>): Promise<void> {
  panel.busy = true;
  panel.text = await load();
  panel.busy = false;
}

✅ Accepted: the flag is cleared in a finally

✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
import { PropagateError } from "@evobug_com/safe-ts-std/result";

class Offline extends PropagateError {}

async function load(): Promise<string> {
  await Promise.resolve();
  if (Date.now() < 0) throw new Offline("offline");
  return "text";
}

interface Panel {
  busy: boolean;
  text: string;
}

export async function reload(panel: Mut<Panel>): Promise<void> {
  panel.busy = true;
  try {
    panel.text = await load();
  } finally {
    panel.busy = false;
  }
}

The same pair made by the functions a function calls counts: enter(mut(room)) and leave(mut(room)) are a pair when the whole program shows that enter always adds one to room.users and leave takes one off.

❌ Rejected: the visit called off on the way leaves its user counted

✕ Rejectedunbalanced-update
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

interface Room {
  users: number;
}

function enter(room: Mut<Room>): void {
  room.users += 1;
}

function leave(room: Mut<Room>): void {
  room.users -= 1;
}

export function visit(room: Mut<Room>, calledOff: boolean): string {
  enter(mut(room));
  if (calledOff) return "called off";
  leave(mut(room));
  return "done";
}
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

interface Room {
  users: number;
}

function enter(room: Mut<Room>): void {
  room.users += 1;
}

function leave(room: Mut<Room>): void {
  room.users -= 1;
}

export function visit(room: Mut<Room>, calledOff: boolean): string {
  enter(room);
  if (calledOff) return "called off";
  leave(room);
  return "done";
}
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

interface Room {
  users: number;
}

function enter(room: Mut<Room>): void {
  room.users += 1;
}

function leave(room: Mut<Room>): void {
  room.users -= 1;
}

export function visit(room: Mut<Room>, calledOff: boolean): string {
  enter(mut(room));
  try {
    if (calledOff) return "called off";
    return "done";
  } finally {
    leave(mut(room));
  }
}
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

interface Room {
  users: number;
}

function enter(room: Mut<Room>): void {
  room.users += 1;
}

function leave(room: Mut<Room>): void {
  room.users -= 1;
}

export function visit(room: Mut<Room>, calledOff: boolean): string {
  enter(room);
  try {
    if (calledOff) return "called off";
    return "done";
  } finally {
    leave(room);
  }
}

The quick fix Undo it in a finally writes this when the undo is a statement of the same block as the change and nothing declared between them is used after it.

What counts ​

  • A pair is two statements of one statement list (a block, the function's body, a loop's body: one round of it), the second after the first, each a change the code shows undoes the other: a boolean place set to true and then to false (or the reverse); x += n and x -= n (++ and --) with the same step; set.add(k) and set.delete(k), map.set(k, v) and map.delete(k) with the same key (item.id and item.id are one key, a const read once stands for its value); push and pop; a place given a new value and then what a const saved of it before (const old = view.mode; view.mode = "edit"; ...; view.mode = old). A change in a branch or a loop whose undo is outside it isn't half of a pair: if (ok) count += 1; is a decision, not a bracket.
  • Through calls: a call is the change its callee always makes to what it is given, once, on every path that completes (room.users += 1 as a statement of its body, with nothing else writing users), with the caller's arguments put in (setMode(lamp, Mode.OFF) sets lamp.mode to Mode.OFF when setMode does lamp.mode = mode). Which functions those are is a summary of each function, worked out once per run by the whole-program pass (the file's own analysis and the editor's ask the same pass). By the loan rules a call changes only what it is lent (mut(room), a parameter declared Mut<...>) and its receiver; a function with no body here, or a method a subclass may override, makes no change the rule can count on (see decided-test for every form of call). A callee's summary describes its own changes; a call the callee makes may change anything it is given, so a change made there is no half of a pair.
  • A call between the two halves is the work the pair brackets, whatever it may change: a function with no body here, a package's call, a function value, a method that may be overridden, a place handed on. The pair stays open across it; what closes it is the undo, written or made by a callee that always makes it. Only the function's own write ends what the rule knows of the pair: a value it can't read (panel.busy = isBusy()), a write it doesn't follow (an element, delete), a new value for what holds the place (panel = next).
  • Places are what outlives the call: a parameter's object, this, module state, a binding of an enclosing function. The function's own locals end with it and aren't followed.
  • A way out is a return, a q(...) that returns the error, a throw, a continue or a break between two halves in a loop's body (the round it ends leaves the change done; the finding says which line), or an await outside a try (or inside one that only has a finally) whose call can fail, as unreleased-resource decides it: a function of the project that propagates a failure, a library's member a pack marks as failing, something the checker can't look up. An await of a function declared to return a promise of a Result, of a value the code uses as a Result (q(await f()), isErr(r)), of a pack's safe version or of Promise.resolve(...) can't reject, and isn't one. A call (or a new) between the two whose callee can throw where it is, as unreleased-resource decides it (a function of the project that throws or propagates a PropagateError, through a Throwing slot too; a built-in that throws, JSON.parse(text); a library's member a pack marks as throwing; a callback the call runs right away that does, list.forEach((x) => check(x))), is one too, whoever catches it: a call that can't fail, std's, or one the checker can't resolve isn't. std's q is known by its import, under whatever name (q as ask).
  • A flag and a return the code writes (or a continue or break): setting a flag and clearing it only after a written early return is often what the function decides there (hide the old error; show a new one only on failure), so a flag is reported on a return only when the function tested the flag before setting it, as a lock: if (this.busy) return; this.busy = true;. A q(...) that returns the error is the failure leaving, not an outcome the author chose for it, as a rejection is: both are reported for every pair. In a checked project panel.text = q(await load()); between the two is the stuck flag's usual spelling.
  • Test files aren't asked: a test that fails on the way out is reported by its runner, and what it leaves behind is its fixture's.

How it relates to the other rules ​

  • unreleased-resource is the same question about a resource the packs name (a timer, a handle, an observer); this rule asks it of state the function changes itself.
  • vue-split-state reports a loading ref set around an await in a component, whatever the way out; this rule reports the way out, anywhere.
  • stale-across-await is about what another call changes during the await; this rule about what the function's own way out leaves.

What the rule can't see ​

  • Pairs split across functions: begin() sets the flag and end() clears it, and the caller calls both. Each function alone has no pair; whether every caller calls end() on every way out is out of this rule's sight by construction.
  • A pair the code doesn't show: an acquire and a release with nothing in common the code shows (lock(name) and unlock(name) that work on a table elsewhere), a library's "call update() after changing items". Those would need a declaration of the pair.
  • A change a callee makes only on some paths, or together with other writes of the same place: no summary says it always happens, so it is no half of a pair. The same for a change made below the callee (enter calls bump, which adds the user).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra