# no-constant-binary-expression

> Disallow comparisons and `&&`, `||`, `??` that always give the same result

Some expressions look like they depend on a value but always give the same result. `a + b ?? c` means `(a + b) ?? c`, and a sum is never `null`, so `c` is never used. `items === []` is always `false`, because `[]` is a new array that isn't equal to anything else.

The rule reports:

- `&&` and `||` where the left side is always truthy or always falsy
- `??` where the left side is never `null` or `undefined`, or always is
- `==`, `!=`, `===` and `!==` where the result is always the same

This rule is on by default.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as an error. To turn it off:

```toml bunfig.toml
[lint.rules]
"no-constant-binary-expression" = "off" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"no-constant-binary-expression" = ["error", {
  checkRelationalComparisons = false,
  allowLiteralToggle = true,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
const label = "Hello, " + user.name ?? "guest";

if (items === []) {
  showEmptyState();
}

const isMissing = !user.email === undefined;
```

Not reported:

{/* good */}
```js
const label = "Hello, " + (user.name ?? "guest");

if (items.length === 0) {
  showEmptyState();
}

const isMissing = user.email === undefined;
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `checkRelationalComparisons` | `false` | Also report `<`, `<=`, `>` and `>=` between two literal values. With an oxlint config, the default is `true`. |
| `allowLiteralToggle` | `true` | Allow `true`, `false`, `0` or `1` on the left of `&&` or `\|\|`. |

`false && ...` is how people switch a piece of code off for a while, so it's allowed by default. Only `bun lint` has `allowLiteralToggle`. With an ESLint config file these are reported, as in ESLint.

{/* good */}
```jsx
const page = <main>{false && <DebugPanel />}</main>;
```

{/* bad {"options": [{"allowLiteralToggle": false}]} */}
```jsx
const page = <main>{false && <DebugPanel />}</main>;
```

{/* bad {"options": [{"checkRelationalComparisons": true}]} */}
```js
const isLarge = 1024 > 4096;
```

{/* good */}
```js
const isLarge = 1024 > 4096;
```
