Skip to content
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

Rule request: forbid control flow statements in finally #5808

Closed
louy opened this issue Apr 8, 2016 · 21 comments · Fixed by #5932
Closed

Rule request: forbid control flow statements in finally #5808

louy opened this issue Apr 8, 2016 · 21 comments · Fixed by #5932
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Comments

@louy
Copy link

louy commented Apr 8, 2016

Adding a return statement in a finally block is a very confusing thing. Thrown errors get ignored, return statements in try blocks get ignored, and your mind explodes.

I'd love to have a rule to forbid this. Especially now that we're having async/await.

examples:

try {
  return 1;
} catch(err) {
  return 2;
} finally {
  return 3;
}
// > 3

try {
  throw new Error;
  return 1;
} catch(err) {
  return 2;
} finally {
  return 3;
}
// > 3
@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Apr 8, 2016
@mysticatea mysticatea added rule Relates to ESLint's core rules feature This change adds a new feature to ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Apr 8, 2016
@mysticatea
Copy link
Member

Thank you for this issue.

Indeed, ReturnStatements in a finally block may hide actual errors or results.
I think also ThrowStatements in a finally block are similar.

As far as I know Java Compiler is warning this ReturnStatements.

I like this idea, but we need to wait for opinions of other members.

@BYK
Copy link
Member

BYK commented Apr 8, 2016

I'm 👍 since this is a core language issue and a pitfall quite easy to fall into without realizing consequences. We should add this to eslint:recommended too IMO.

@nzakas
Copy link
Member

nzakas commented Apr 11, 2016

Seems like a good idea. Does anyone want to champion this? @eslint/eslint-team

@BYK we only change eslint:recommended when we do a major release because it's a breaking change.

@michaelficarra
Copy link
Member

If return is forbidden, all control flow operations should be forbidden: throw, break, and continue.

@platinumazure
Copy link
Member

👍 to @michaelficarra. Perhaps the rule could be called something like no-finally-control-flow?

@BYK
Copy link
Member

BYK commented Apr 12, 2016

@nzakas - I know. I wanted to raise the flag to include this in 3.0.0 planning :)

I can champion the rule. @onurtemizkan interested in implementing?

@onurtemizkan
Copy link
Contributor

Sure! 😄

@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Apr 12, 2016
@nzakas
Copy link
Member

nzakas commented Apr 12, 2016

Okay @BYK is championing this rule. Please work with @onurtemizkan to implement the rule using the new format.

@louy louy changed the title Rule request: forbid return in finally Rule request: forbid control flow statements in finally Apr 13, 2016
@BYK
Copy link
Member

BYK commented Apr 15, 2016

Did we decide on a rule name?

no-finally-control-flow sounds like a good one but a bit on the longer side. Other suggestions: no-control-finally, no-control-in-finally, no-unpredictable-finally.

@onurtemizkan has started working on this.

@platinumazure
Copy link
Member

I could probably get behind any of those. I'll throw one more name into the
ring: no-interrupt-finally.
On Apr 15, 2016 8:39 AM, "Burak Yiğit Kaya" [email protected]
wrote:

Did we decide on a rule name?

no-finally-control-flow sounds like a good one but a bit on the longer
side. Other suggestions: no-control-finally, no-control-in-finally,
no-unpredictable-finally.

@onurtemizkan https://github.com/onurtemizkan has started working on
this.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#5808 (comment)

@nzakas
Copy link
Member

nzakas commented Apr 15, 2016

"Control flow" refers to anything that changes where execution flows, Including if, while, etc., so it's a bit broad for this use case.

How about no-unsafe-finally?

@BYK
Copy link
Member

BYK commented Apr 16, 2016

no-unsafe-finally may be a bit too broad but I like it.

@nzakas
Copy link
Member

nzakas commented Apr 18, 2016

Okay, we'll leave this to @BYK and @onurtemizkan to implement.

onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 22, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 22, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 22, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 22, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 22, 2016
@onurtemizkan
Copy link
Contributor

@michaelficarra I couldn't find a case for break and continue, creating unexpected behaviour in finally. Could you give an example?

@michaelficarra
Copy link
Member

(function() {
  label: try {
    return 0;
  } finally {
    break label;
  }
  return 1;
})()

@onurtemizkan
Copy link
Contributor

Ah, okay. Thanks!

onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 25, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 25, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 26, 2016
onurtemizkan added a commit to onurtemizkan/eslint that referenced this issue Apr 26, 2016
@bakkot
Copy link
Contributor

bakkot commented Apr 26, 2016

Currently this rule isn't actually checking that breaks and continues are crossing the boundary of the finally. In fact, one of the examples of correct code from the docs actually gives an error:

let foo = function(a) {
    try {
        return 1;
    } catch(err) {
        return 2;
    } finally {
        switch(a) {
            case 1: {
                console.log("hola!")
                break;
            }
        }
    }
};

This also trips this rule, I think incorrectly:

try {} finally {
  while(true) {
    break;
  }
}

I would not think the mere presence of control flow statements would be an issue: it's only control flow which crosses the boundary of the finally. Doing this right involves determining which statements are broken / continued.

I can open an issue for this if people agree.

@platinumazure
Copy link
Member

@bakkot I agree with your logic. Please open a new issue and I will triage it as a bug right away. 😄

@onurtemizkan
Copy link
Contributor

@bakkot Sorry, my bad. I can post a fix for this if you open an issue.

@bakkot
Copy link
Contributor

bakkot commented Apr 26, 2016

Opened #5972.

@mk-pmb
Copy link

mk-pmb commented Aug 5, 2016

Does anyone have an in-the-wild example where flow control in a finally block caused an actual problem? (To me, the use of this rule seems intuitive, but I'd like to convince a skeptic.)

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants