no-unused-expressions
Disallow statements that compute a value and throw it away
A statement such as server.close; or count === 0; computes a value and does nothing with it. It's usually a mistake, like a call that's missing its parentheses, or a === where you meant =.
Calls, new, assignments, await, yield, delete, void, i++, and directives such as "use strict" do something, so they aren't reported. Neither are isReady && start(); and isReady ? start() : wait();, which are short ways to write an if.
Configure#
Off by default. To turn it on:
[lint.rules]
"no-unused-expressions" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"no-unused-expressions" = ["error", {
allowShortCircuit = true,
allowTernary = true,
allowTaggedTemplates = false,
enforceForJSX = false,
ignoreDirectives = false,
}]Examples#
server.close;Expected an assignment or function call and instead saw an expression.
count === 0;Expected an assignment or function call and instead saw an expression.
`Hello, ${name}`;Expected an assignment or function call and instead saw an expression."use strict";
server.close();
count = 0;
console.log(`Hello, ${name}`);Options#
| Option | Default | What it does |
|---|---|---|
allowShortCircuit | true | Allow a && b(), a || b() and a ?? b(), if the right side would be allowed as a statement. |
allowTernary | true | Allow a ? b() : c(), if both branches would be allowed as statements. |
allowTaggedTemplates | false | Allow a tagged template as a statement. |
enforceForJSX | false | Also report a JSX element as a statement. |
ignoreDirectives | false | Allow strings at the start of a file or a function when ecmaVersion is 3, which has no directives. |
With an ESLint config file the defaults are ESLint's: every option is false.
By default, you can use && or ? : instead of an if:
isReady && start();
isReady ? start() : wait();The right side still has to do something:
isReady && start;Expected an assignment or function call and instead saw an expression.
isReady ? start : wait;Expected an assignment or function call and instead saw an expression.{ "allowShortCircuit": false }1 problemisReady && start();Expected an assignment or function call and instead saw an expression.{ "allowTernary": false }1 problemisReady ? start() : wait();Expected an assignment or function call and instead saw an expression.A tag is a function, so a tagged template can have an effect:
{ "allowTaggedTemplates": true }No problemssql`DELETE FROM sessions`;{ "enforceForJSX": true }1 problem<Spinner />;Expected an assignment or function call and instead saw an expression.