-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: correct no-useless-return behaviour in try statements #16996
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
lib/rules/no-useless-return.js
Outdated
if (scopeInfo.traversedTryBlockStatements && scopeInfo.traversedTryBlockStatements.length > 0) { | ||
const blockInitialRange = scopeInfo.traversedTryBlockStatements[0].range[0]; | ||
const blockFinalRange = scopeInfo.traversedTryBlockStatements[0].range[1]; | ||
const returnInitialRange = node.range[0]; | ||
const returnFinalRange = node.range[1]; | ||
|
||
if (returnInitialRange >= blockInitialRange && returnFinalRange <= blockFinalRange) { | ||
continue; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should check all try block statements in the list instead of just the first one. Otherwise, this would still be a false negative:
function foo() {
try {
bar();
} catch {
try {
baz();
return; // false negative
} catch {
qux();
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I believe this is a new false positive:
function foo() {
try {
bar();
return; // false positive
} finally {
baz();
}
qux();
}
I'm not sure what exactly is causing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what exactly is causing it.
I'm looking into it. I suspect the TryStatement:exit()
query is not working correctly if there is finally
instead of catch
.
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. |
I was away on vacation. I'll take a look this week. Currently stuck at a false positive #16996 (comment) |
I spent some time on this but couldn't figure out why we have this new false positive #16996 (comment). @mdjermanovic Do you have any idea? |
The problem is the overly eager caching of Also, we should not discard all useless returns associated to a segment if only some of them are actually being marked as used. I've added a commit to show a possible fix. Please, just edit my changes as required to move on with this PR. |
@fasttime Thanks for the update 🙌🏻. @mdjermanovic Can you review it once again? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
There's a merge conflict. |
cd6cb7b
to
80c7367
Compare
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [eslint](https://eslint.org) ([source](https://github.com/eslint/eslint)) | devDependencies | minor | [`8.41.0` -> `8.42.0`](https://renovatebot.com/diffs/npm/eslint/8.41.0/8.42.0) | --- ### Release Notes <details> <summary>eslint/eslint</summary> ### [`v8.42.0`](https://github.com/eslint/eslint/releases/tag/v8.42.0) [Compare Source](eslint/eslint@v8.41.0...v8.42.0) #### Features - [`b8448ff`](eslint/eslint@b8448ff) feat: correct no-useless-return behaviour in try statements ([#​16996](eslint/eslint#16996)) (Nitin Kumar) #### Bug Fixes - [`a589636`](eslint/eslint@a589636) fix: Config with `ignores` and without `files` should not always apply ([#​17181](eslint/eslint#17181)) (Milos Djermanovic) - [`c4fad17`](eslint/eslint@c4fad17) fix: Correct ignore message for "node_modules" subfolders ([#​17217](eslint/eslint#17217)) (Francesco Trotta) #### Documentation - [`01d7142`](eslint/eslint@01d7142) docs: Update README (GitHub Actions Bot) - [`e5182b7`](eslint/eslint@e5182b7) docs: Update README (GitHub Actions Bot) #### Chores - [`6ca5b7c`](eslint/eslint@6ca5b7c) chore: upgrade [@​eslint/js](https://github.com/eslint/js)[@​8](https://github.com/8).42.0 ([#​17236](eslint/eslint#17236)) (Milos Djermanovic) - [`67fc5e7`](eslint/eslint@67fc5e7) chore: package.json update for [@​eslint/js](https://github.com/eslint/js) release (ESLint Jenkins) - [`0892412`](eslint/eslint@0892412) refactor: remove `Identifier` listener in no-irregular-whitespace ([#​17235](eslint/eslint#17235)) (Milos Djermanovic) - [`f67d298`](eslint/eslint@f67d298) test: Add `FlatESLint` tests with missing config files ([#​17164](eslint/eslint#17164)) (Milos Djermanovic) - [`5b68d51`](eslint/eslint@5b68d51) chore: Fix `fixedsize` attribute in code path analysis DOT debug output ([#​17202](eslint/eslint#17202)) (Milos Djermanovic) - [`37432f2`](eslint/eslint@37432f2) chore: update descriptions in key-spacing tests ([#​17195](eslint/eslint#17195)) (Milos Djermanovic) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xMTMuMCIsInVwZGF0ZWRJblZlciI6IjM1LjExMy4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCJ9--> Co-authored-by: cabr2-bot <[email protected]> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1923 Reviewed-by: Epsilon_02 <[email protected]> Co-authored-by: Calciumdibromid Bot <[email protected]> Co-committed-by: Calciumdibromid Bot <[email protected]>
Prerequisites checklist
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)
Fixes #7481
Implemented the solution as suggested in #16593 (comment)
Is there anything you'd like reviewers to focus on?
No