# @typescript-eslint/no-confusing-void-expression

> Require expressions of type `void` to stand alone as statements

A function that returns `void` has no result to use. Code like `const saved = save(user)` or `return log(error)` suggests there is one, and the reader goes looking for a value that doesn't exist. A `void` call belongs on its own line.

A `void` call is still fine on the right of `&&`, `||` and `??`, and in the branches of `? :`, if the whole expression is a statement.

`--fix` adds braces to an arrow function, and takes the call out of a `return`. A `void` value that's assigned or passed on is reported but not fixed.

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

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/no-confusing-void-expression" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/no-confusing-void-expression" = ["error", {
  ignoreArrowShorthand = false,
  ignoreVoidOperator = false,
  ignoreVoidReturningFunctions = false,
}]
```

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

## Examples

Reported:

{/* bad {"count": 3} */}
```ts
declare function log(message: string): void;

const result = log("saved");

const onSave = () => log("saved");

function onError(error: Error) {
  return log(error.message);
}
```

Not reported:

{/* good */}
```ts
declare function log(message: string): void;
declare const verbose: boolean;

log("saved");
verbose && log("details");

const onSave = () => {
  log("saved");
};

function onError(error: Error) {
  log(error.message);
}
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `ignoreArrowShorthand` | `false` | Allow `() => log()`, an arrow function without braces. |
| `ignoreVoidOperator` | `false` | Allow a `void` expression anywhere if it's marked with the `void` operator. `--fix` then adds `void` in arrow functions and returns. |
| `ignoreVoidReturningFunctions` | `false` | Allow returning a `void` expression from a function that returns `void` itself. |

{/* good {"options": [{"ignoreArrowShorthand": true}]} */}
```ts
declare function log(message: string): void;

["a", "b"].forEach(name => log(name));
```

With `ignoreVoidOperator`:

{/* good {"options": [{"ignoreVoidOperator": true}]} */}
```ts
declare function log(message: string): void;

const onSave = () => void log("saved");
```

{/* bad {"options": [{"ignoreVoidOperator": true}]} */}
```ts
declare function log(message: string): void;

const onSave = () => log("saved");
```

With `ignoreVoidReturningFunctions`, the return type can be written on the function or come from where the function is used:

{/* good {"options": [{"ignoreVoidReturningFunctions": true}]} */}
```ts
declare function log(message: string): void;

function onError(error: Error): void {
  return log(error.message);
}

["a", "b"].forEach(name => log(name));
```

{/* bad {"options": [{"ignoreVoidReturningFunctions": true}]} */}
```ts
declare function log(message: string): void;

function onError(error: Error) {
  return log(error.message);
}
```
