Skip to content

Add no-unused-array-method-return rule#2940

Open
sindresorhus wants to merge 5 commits intomainfrom
no-unused-array-method-return
Open

Add no-unused-array-method-return rule#2940
sindresorhus wants to merge 5 commits intomainfrom
no-unused-array-method-return

Conversation

@sindresorhus
Copy link
Copy Markdown
Owner

Fixes #741

@sindresorhus sindresorhus force-pushed the no-unused-array-method-return branch from c5eaa6e to 6af1ec2 Compare April 13, 2026 09:02
@silverwind
Copy link
Copy Markdown
Contributor

silverwind commented Apr 18, 2026

Critical: rule crashes on common code

rules/no-unused-array-method-return.js:251getVariableValue returns definition.node.init unconditionally. When a declarator has no initializer (uninitialized let, for…of, for…in, for (let x; …)), init is null. resolveReceiver then recurses with null and throws TypeError: Cannot read properties of null (reading 'type') at line 171.

Reproduced via npx ava test/no-unused-array-method-return.js after dropping these inputs into the existing test file:

'let array; array.map(fn);'                    // CRASH
'for (const x of arr) x.map(fn);'              // CRASH
'for (let x of arr) x.map(fn);'                // CRASH
'for (var x of arr) x.map(fn);'                // CRASH
'for (const x in obj) x.map(fn);'              // CRASH
'for (let x; x.length;) x.map(fn);'            // CRASH

Suggested fix: add && definition.node.init to the condition at lines 244–250, returning uncertainValue for these cases (matches the documented "for…of bindings unsupported" intent). Worth adding at least one for…of and one bare let x; to the test file to prevent regression.

Code quality

  • L9–40 — methodNames is only used to build methods; inline as const methods = new Set([...]);.
  • L224–238 and L289–304 — ~30 lines of inline boundary commentary largely repeat what the docs already explain. Trimming would match the terser style of nearby rules.
  • Mix of const x = (…) => and function x(…) declarations in the same file is inconsistent.

Heuristic gaps

  • .slice / .at / .includes / .indexOf / .concat / .lastIndexOf exist on strings; unresolved receivers will false-positive. Acknowledged in the docs, but worth a final risk check before promoting beyond unopinionated.
  • Statement-level SequenceExpression (e.g. sideEffect(), arr.map(fn);) is silently skipped — listed as intentional but a real false negative.

Review by Claude Opus 4.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rule proposal: no-unused-return-value

2 participants