noUnusedExpressions
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noUnusedExpressions
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-unused-expressions
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUnusedExpressions": "error" } } }}
Description
Section titled “Description”Disallow expression statements that are neither a function call nor an assignment.
When an expression is used as a statement, it should be explicitly clear
what the intention behind the expression is. This is clear for function
calls and assignments, because the call or the assignment itself is the
primary intention behind the statement. For other expression kinds, the
intention is much more ambiguous; it could be the expression contains
side-effects that are not very explicit, but it could also be that it is
an error where the author forgot to use the result of the expression,
such as a forgotten return
keyword, or it could point to a function
that the author forgot to call.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”0
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ 0
│ ^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
if(0) 0
code-block.js:1:7 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ if(0) 0
│ ^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
{0}
code-block.js:1:2 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ {0}
│ ^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
f(0), {}
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ f(0), {}
│ ^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
a && b()
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ a && b()
│ ^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
a, b()
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ a, b()
│ ^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
c = a, b
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ c = a, b
│ ^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
a() && function namedFunctionInExpressionContext () {f();}
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ a() && function namedFunctionInExpressionContext () {f();}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
(function anIncompleteIIFE () {});
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ (function anIncompleteIIFE () {});
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
injectGlobal`body{ color: red; }`
code-block.js:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ injectGlobalbody{ color: red; }
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
Set<number>
code-block.ts:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ Set<number>
│ ^^^^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
1 as number
code-block.ts:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ 1 as number
│ ^^^^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
window!
code-block.ts:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ window!
│ ^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
JSX expressions are considered invalid when used as a statement too:
<MyComponent />
code-block.jsx:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ <MyComponent />
│ ^^^^^^^^^^^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
<></>
code-block.jsx:1:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
> 1 │ <></>
│ ^^^^^
2 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
{} // In this context, this is a block statement, not an object literal
{ myLabel: foo() } // In this context, this is a block statement with a label and expression, not an object literal
function namedFunctionDeclaration () {}
(function aGenuineIIFE () {}());
f()
a = 0
new C
delete a.b
void a
Handling of Directives
Section titled “Handling of Directives”Any stand-alone string at the start of a script, module, or function is considered a directive and is therefore allowed.
"use strict";"use asm""use stricter";"use babel""any other strings like this in the directive prologue";"this is still the directive prologue";
function foo() { "bar";}
class Foo { someMethod() { "use strict"; }}
The following are not considered valid directives:
doSomething();"use strict"; // this isn't in a directive prologue, because there is a non-directive statement before it
code-block.js:2:1 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
1 │ doSomething();
> 2 │ “use strict”; // this isn’t in a directive prologue, because there is a non-directive statement before it
│ ^^^^^^^^^^^^^
3 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
function foo() { "bar" + 1;}
code-block.js:2:5 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
1 │ function foo() {
> 2 │ “bar” + 1;
│ ^^^^^^^^^
3 │ }
4 │
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
class Foo { static { "use strict"; // class static blocks do not have directive prologues }}
code-block.js:3:9 lint/nursery/noUnusedExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected an assignment or function call but found an expression instead.
1 │ class Foo {
2 │ static {
> 3 │ “use strict”; // class static blocks do not have directive prologues
│ ^^^^^^^^^^^^^
4 │ }
5 │ }
ℹ This expression may be unintentionally unused or it might be a function that you forgot to call.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.