Skip to content

fix: false positive for reference in finally block#20655

Merged
fasttime merged 9 commits into
eslint:mainfrom
Tanujkanti4441:fix-finally-ref
May 22, 2026
Merged

fix: false positive for reference in finally block#20655
fasttime merged 9 commits into
eslint:mainfrom
Tanujkanti4441:fix-finally-ref

Conversation

@Tanujkanti4441

@Tanujkanti4441 Tanujkanti4441 commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Prerequisites checklist

AI acknowledgment

  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Added a condition in no-useless-assignment rule to allow the read reference in finally block. now it will not report following case:

Added a method makeFirstThrowablePathInCatchBlock in code-path-state to handle the possibility of throwing an error by a CallExpression , ImportExpression, NewExpression, MemberExpression and identifier refereces in catch block with a preceding finally block. such as

/* eslint no-useless-assignment: "error" */
function main() {
  let outcome = 'unknown';

  try {
    helper1();
    outcome = 'success';
  } catch (err) {
    helper2();
    outcome = 'exception'; 
  } finally {
    console.log(outcome);
  }
}

function helper1() {
  throw Error('helper1');
}

function helper2() {
  throw Error('helper2');
}

main();

Is there anything you'd like reviewers to focus on?

Fixes #20583 Fixes #20637

@Tanujkanti4441 Tanujkanti4441 requested a review from a team as a code owner March 21, 2026 13:37
@github-project-automation github-project-automation Bot moved this to Needs Triage in Triage Mar 21, 2026
@netlify

netlify Bot commented Mar 21, 2026

Copy link
Copy Markdown

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit d55f7c5
🔍 Latest deploy log https://app.netlify.com/projects/docs-eslint/deploys/6a10331505e8ee0008575470

@eslint-github-bot eslint-github-bot Bot added the bug ESLint is working incorrectly label Mar 21, 2026
@github-actions github-actions Bot added the rule Relates to ESLint's core rules label Mar 21, 2026
@jookira

jookira commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

@Tanujkanti4441 You need to claim an issue before working on it. Please take a look at https://eslint.org/docs/latest/contribute/work-on-issue

@Tanujkanti4441

Tanujkanti4441 commented Mar 21, 2026

Copy link
Copy Markdown
Contributor Author

Hey @ayatobai, thanks for the comment, the thought behind the PR is to just provide a quick fix as this issue has been reported twice within a month in #20583 and #20637.

@fasttime fasttime moved this from Needs Triage to Triaging in Triage Mar 27, 2026
Comment thread lib/rules/no-useless-assignment.js Outdated
Comment on lines +246 to +268
const isRefInFinally =
targetAssignment.variable.references.some(ref => {
const id = ref.identifier;

const hasRefInFinally =
ref.isRead() &&
target.tryStatementBlocks.some(tryBlock => {
const tryNode = tryBlock.parent;

return (
tryNode?.handler &&
tryNode?.finalizer?.range[0] <=
id.range[0] &&
id.range[1] <= tryNode?.finalizer?.range[1]
);
});

return hasRefInFinally;
});

if (isRefInFinally) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will produce false negatives when the reference in the finally block is unreachable, for example:

function main() {
	let outcome = 'unknown';

	try {
		helper1();
	} catch (err) {
		helper2();
		outcome = 'exception'; 
	} finally {
		return;
		console.log(outcome);
	}
}

Playground link

Currently, two problems are being reported, but after the change there will be none.

@github-actions

github-actions Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions Bot added the Stale label Apr 6, 2026
@DMartens

Copy link
Copy Markdown
Contributor

Closing this in favor of #20665 which fixes the problem in the code path analyzer.

@DMartens DMartens closed this Apr 11, 2026
@github-project-automation github-project-automation Bot moved this from Triaging to Complete in Triage Apr 11, 2026
@Tanujkanti4441

Copy link
Copy Markdown
Contributor Author

Hey @DMartens, I think we still have the issue that is reported in #20637, I closed that one as I thought we could solve both issues with this PR. So, is it okay if I reopen and continue to work on this PR to resolve the problems, such as:

/* eslint no-useless-assignment: "error" */
function main() {
  // False positive: no-useless-assignment.
  // If `helper2()` throws an exception, then the 'unknown' value will get used.
  let outcome = 'unknown';

  try {
    helper1();
    outcome = 'success';
  } catch (err) {
    helper2();
    outcome = 'exception'; 
  } finally {
    console.log(outcome);
  }
}

function helper1() {
  throw Error('helper1');
}

function helper2() {
  throw Error('helper2');
}

main();

@DMartens

Copy link
Copy Markdown
Contributor

Oh I am sorry for prematurely closing.
I think we should handle the open case also in the code path analyzer, so other rules using it also benefit from the changes.
We handle the CallExpression here.
This does not handle the case when throwing an error in a catch block that the corresponding finally block is still executed.

@DMartens DMartens reopened this Apr 13, 2026
@github-project-automation github-project-automation Bot moved this from Complete to Evaluating in Triage Apr 13, 2026
@github-actions github-actions Bot removed the Stale label Apr 13, 2026
@github-actions github-actions Bot added core Relates to ESLint's core APIs and features and removed rule Relates to ESLint's core rules labels Apr 23, 2026
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions Bot added the Stale label May 6, 2026
@fasttime

fasttime commented May 7, 2026

Copy link
Copy Markdown
Member

This isn't stale. It needs to be reviewed.

@fasttime fasttime removed the Stale label May 7, 2026
Comment thread lib/linter/code-path-analysis/code-path-analyzer.js Outdated
Comment thread lib/linter/code-path-analysis/code-path-analyzer.js Outdated
Comment thread lib/linter/code-path-analysis/code-path-analyzer.js Outdated
Comment thread tests/lib/rules/no-useless-assignment.js
@fasttime fasttime added the accepted There is consensus among the team that this change meets the criteria for inclusion label May 22, 2026
@fasttime fasttime moved this from Evaluating to Implementing in Triage May 22, 2026
Comment thread lib/linter/code-path-analysis/code-path-state.js Outdated
Tanujkanti4441 and others added 2 commits May 22, 2026 16:02

@fasttime fasttime left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@fasttime fasttime merged commit c5bc78b into eslint:main May 22, 2026
43 checks passed
@github-project-automation github-project-automation Bot moved this from Implementing to Complete in Triage May 22, 2026
@Tanujkanti4441 Tanujkanti4441 deleted the fix-finally-ref branch May 23, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features

Projects

Status: Complete

4 participants