Skip to content

noMixedReturns (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noMixedReturns": "error"
}
}
}
}

Require return statements in a function to either always or never return a value.

When only some return statements in a function return a value, code that calls the function can get undefined when it doesn’t expect it.

This only looks at syntax, not types. Typescript has a built in check noImplicitReturns, which is recommended if you want type information to be taken into account.

function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
code-block.js:5:9 lint/nursery/noMixedReturns ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This return doesn't return a value, but another return in this function does.

3 │ return true;
4 │ } else {
> 5 │ return;
│ ^^^^^^^
6 │ }
7 │ }

ℹ This return returns a value.

1 │ function doSomething(condition) {
2 │ if (condition) {
> 3 │ return true;
│ ^^^^^^^^^^^^
4 │ } else {
5 │ return;

ℹ A function that returns a value on some paths gives undefined on the others, which is easy to overlook.

ℹ Make every return in this function return a value, or none of them. If the function should give undefined, write return undefined.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function doSomethingElse(condition) {
if (condition) {
return true;
}
}
code-block.js:1:1 lint/nursery/noMixedReturns ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function can reach its end without returning a value, but it returns a value elsewhere.

> 1 │ function doSomethingElse(condition) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ if (condition) {
3 │ return true;

ℹ This return returns a value.

1 │ function doSomethingElse(condition) {
2 │ if (condition) {
> 3 │ return true;
│ ^^^^^^^^^^^^
4 │ }
5 │ }

ℹ A function that returns a value on some paths gives undefined on the others, which is easy to overlook.

ℹ Add a return with a value at the end of the function. If the function should give undefined, write return undefined.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
function doSomethingElse(condition) {
if (condition) {
return true;
}
return undefined;
}
function Foo() {
if (!(this instanceof Foo)) {
return new Foo();
}
this.a = 0;
}