Rule proposal: `no-unused-return-value`
See original GitHub issuePlease describe what the rule should do:
Detect unused return values of built-in object methods where it never makes sense not to use the return value.
What category of rule is this? (place an “X” next to just one item)
[x] Suggests an alternate way of doing something (suggestion)
Provide 2-3 code examples that this rule will warn about:
const arr = [{name: 'eslint'}];
arr.map(item => item.name); // error: Unused Array.prototype.map return value
const arr = [{name: 'eslint'}];
arr.filter(item => item.name === 'eslint'); // error: Unused Array.prototype.filter return value
let i = 0;
const arr = [1,2,3];
arr.map((item) => { // error: Unused Array.prototype.map return value
i += 1;
});
Object.create(null); // error: Unused Object.create return value
Why should this rule be included in ESLint (instead of a plugin)?
It is useful to anyone using built-in JS objects and especially for iteration methods that return a value like Array.prototype.map
. If its return value is unused, it would be better to refactor such cases to Array.prototype.forEach
to make the intend clear.
I could see the rule being especially useful to beginners who might not have memorized yet that Array.prototype.filter
or Array.prototype.map
do not modify the array.
The concept can be applied to any built-in static or prototype method that either returns a value or does iteration only. It can be extended to other built-ins like String
, Number
, Map
, Set
and more.
For Array
and Object
, I see these suitable methods:
Array.isArray
Array.of
Array.prototype.concat
Array.prototype.filter
Array.prototype.includes
Array.prototype.indexOf
Array.prototype.join
Array.prototype.lastIndexOf
Array.prototype.slice
Array.prototype.toSource
Array.prototype.toString
Array.prototype.toLocaleString
Array.prototype.entries
Array.prototype.every
Array.prototype.find
Array.prototype.findIndex
Array.prototype.keys
Array.prototype.map
Array.prototype.some
Array.prototype.values
Object.create
Object.entries
Object.fromEntries
Object.getOwnPropertyDescriptor
Object.getOwnPropertyDescriptors
Object.getOwnPropertyNames
Object.getOwnPropertySymbols
Object.getPrototypeOf
Object.is
Object.isExtensible
Object.isFrozen
Object.isSealed
Object.keys
Object.values
Object.prototype.hasOwnProperty
Object.prototype.isPrototypeOf
Object.prototype.propertyIsEnumerable
Object.prototype.toLocaleString
Object.prototype.toString
Related previous discussion: https://github.com/eslint/eslint/issues/12269
Are you willing to submit a pull request to implement this rule?
No
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (10 by maintainers)
Top GitHub Comments
Array.from(iterableOrArraylike, forEachFunction)
Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.
Thanks for contributing to ESLint and we appreciate your understanding.