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
New rule proposal: no-unreachable-loop #12381
Comments
For for (var k in obj) {
k = true;
break;
}
if (k) {
// object has enumerable properties
} The same could be true of |
That's correct, I think there should be options to turn off An alternative for an user is to extract that code to a function ( I'd still vote that the rule checks |
Most configurable would be an option for each construct, regardless of the defaults. |
I agree, one option for Or did you mean all 5 options, |
Each option separately. |
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 |
@ilyavolodin Mind clarifying if your thumb on the comment above is in support of this proposal? If so, we can mark this as accepted :) |
One more 👍 to accept, if thumb on the comment doesn't count :) |
I'm working on this. |
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
andfor-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:
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 indo-while
test andfor-loop
update, but I think thatno-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 lastbreak
, 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.
The text was updated successfully, but these errors were encountered: