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

no-fallthrough warns when infinite loop with no break precludes fallthrough #1156

Open
peetklecha opened this issue May 6, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@peetklecha
Copy link

Lint Name

no-fallthrough

Code Snippet

function* sequence(type: "even" | "odd" | "prime") {
  let num = 0;
  switch (type) {
    case "even":
      while (true) {
        if (isEven(num)) yield num;
        num += 1;
      }
      // break;
    case "odd":
      ...
  }
}

Expected Result

There should be no warning.

Actual Result

Fallthrough is not allowed
Add `break` or comment `/* falls through */` to your case statementdeno-lint(no-fallthrough)

Uncommenting the break causes that to receive an unreachable code warning, which shows that the linter already identifies the loop as never breaking.

Additional Info

Same basic issue as #1042 but this should be even more straightforward to fix.

Version

deno 1.33.2 (release, x86_64-apple-darwin)
v8 11.4.183.1
typescript 5.0.3

@peetklecha peetklecha added the bug Something isn't working label May 6, 2023
@magurotuna
Copy link
Member

I think the core of the issue can be seen in the following snippet:

switch (someValue) {
  case 0:
    while (true) {}
  default:
    console.log(42);
}

The control flow analyzer does know while (true) {} is an infinite loop. However, this information is not utilized well by no-fallthrough rule - maybe the straightforward way to solve this would be that we let no-fallthrough skip the fallthrough check if the final point of a case (i.e. in the above example, the point right after while (true) {} but before going into the next branch, default:) is unreachable. This may require some fix in control flow analyzer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants