Skip to content

negative-member-guard ​

x !== E.A on a closed set guards work, in a branch that is a case of its own: every member but A takes it, and so does every member added to the set later. Reported when the test also compares another value with a member (u.phase === Phase.INSIDE && u.job !== Job.FLEE), or excludes two members or more (x !== E.A && x !== E.B). A closed set is an enum's tag, a union of literals, or the constants of a code object, as for exhaustive-dispatch.

Why: the test names the members the work is not for. The members it is for are "all the others", a list nobody wrote down, which grows on its own. An adopter's rule change kept a unit's work inside a building with u.phase === Phase.INSIDE && u.job !== Job.FLEE: experiments and lessons, not refugees. WORSHIP was added to Job later, had no work, and took that branch. A set that names its members makes a new member a decision: it is in the set, or it isn't. Guide: Enums.

❌ Rejected: a job added later keeps work it doesn't have

ts
export const Job = Object.freeze({ STUDY: 0, TEACH: 1, FLEE: 2, WORSHIP: 3 });
export type Job = (typeof Job)[keyof typeof Job];
export const Phase = Object.freeze({ WALK: 0, INSIDE: 1 });
export type Phase = (typeof Phase)[keyof typeof Phase];

type Unit = { readonly phase: Phase; readonly job: Job; readonly carry: number };

export function keptWork(u: Unit, cycle: number): number {
  if (u.phase === Phase.INSIDE && u.job !== Job.FLEE) {
    return Math.min(u.carry, cycle - 1);
  }
  return 0;
}

✅ Accepted: the jobs that keep work, named once

✓ Accepted0 diagnostics
ts
export const Job = Object.freeze({ STUDY: 0, TEACH: 1, FLEE: 2, WORSHIP: 3 });
export type Job = (typeof Job)[keyof typeof Job];
export const Phase = Object.freeze({ WALK: 0, INSIDE: 1 });
export type Phase = (typeof Phase)[keyof typeof Phase];

/** Jobs with work inside a building, kept when the rules change. */
export const KEEPS_WORK: ReadonlySet<Job> = new Set([Job.STUDY, Job.TEACH]);

type Unit = { readonly phase: Phase; readonly job: Job; readonly carry: number };

export function keptWork(u: Unit, cycle: number): number {
  if (u.phase === Phase.INSIDE && KEEPS_WORK.has(u.job)) {
    return Math.min(u.carry, cycle - 1);
  }
  return 0;
}

A switch ending in default: return unreachable(u.job) works too, and makes the compiler ask about each new member.

What counts ​

  • Reported: an if (or else if) whose test excludes members of one value with !== or !=, joined by && to the rest of the test, where the test also compares another value with a member, or excludes two members or more of the same value; and the branch does work.
  • Not reported:
    • an early exit for the excluded member: a branch that only returns nothing or a literal (return;, return 0), returns a constant (return SimError.NOT_ADULT), continues, breaks or throws;
    • one exclusion alone, if (u.phase !== Phase.IDLE) leave(u): that reads as "everything but IDLE", and on an adopter's 35,000-line simulation it was nearly always meant so (34 such tests, 9 reported);
    • an exclusion inside an || or a !(...), a while, a ?:.
  • The set comes from the value's declared type, or, when it is typed number or string, from the object the excluded constants are read from.

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