question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule proposal: no-unreachable-loop

See original GitHub issue

Please describe what the rule should do:

Disallows loops which body exits the loop in all paths and therefore can never even reach the test condition for the second time (or first time if it’s do-while), meaning that it isn’t actually a loop.

Targets while, do-while, for, for-in and for-of loops.

What category of rule is this? (place an “X” next to just one item)

[X] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Enforces code style (layout) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

while (foo) {
    doSomething(foo);
    foo = foo.parent;
    break;
}

for (i = 0; i < arr.length; i++) {
    if (isSomething(arr[i])) {
        break;
    } else {
        return false;
    }
}

do {
    if (foo.type === "bar") {
        return foo;
    } else {
        throw new Error("invalid");
    }
} while (foo);

for (foo of bar) {
    if (foo.type === "bar") {
        doSomething(foo);
    }
    break;
}

for (foo in bar) break;

The code is correct only if the end of the body is reachable (i.e. loops from the end) or the loop has a continue statement.

Why should this rule be included in ESLint (instead of a plugin)?

It’s a very possible error in the code, something is missing or something else is at the wrong place.

Even if it isn’t an error the code should be refactored to avoid unnecessary loop syntax.

This is a similar type of error as no-unreachable, but I think this should be a separate rule because there is no particular unreachable code in these cases (except for the code in do-while test and for-loop update, but I think that no-unreachable doesn’t report that).

In some cases both no-unreachable and this rule can report warnings (e.g. no-unreachable for code after the last break, this rule for the whole loop), but I think that isn’t overlapping.

Are you willing to submit a pull request to implement this rule?

Yes, I’d love to work on this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
mdjermanoviccommented, Nov 13, 2019

Any thoughts from the team about this rule? 😃

It looks to me that using the already existing code path analysis features this rule could reliably (without false negatives or false positives) detect these bugs in the code, and might be a nice complement to the no-unreachable rule.

1reaction
mdjermanoviccommented, Dec 11, 2019

I’m working on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-unreachable-loop - ESLint - Pluggable JavaScript Linter
This rule aims to detect and disallow loops that can have at most one iteration, by performing static code path analysis on loop...
Read more >
Safety Standard for Operating Cords on Custom Window ...
The final rule does not preclude manufacturers from developing new methods of meeting the “inaccessible” requirement in section 4.3.1 of ANSI/ ...
Read more >
Khan Academy Algorithms List Flashcards - Quizlet
Which change will reduce the most number of operations while still outputting a correct answer? Moving the calculation of variance to be after...
Read more >
D87149 [InstCombine] erase instructions leading up to ... - LLVM
The first point is the important one. unreachable should eat any preceeding instruction that transfers execution for sure. We should not remove ...
Read more >
CoffeeScript
The golden rule of CoffeeScript is: “It's just JavaScript.” The code compiles one-to-one into the equivalent JS, and there is no interpretation at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found