You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Warning when writing a generator function without any yield #13847
Expected behavior:
Some kind of notice that this generator function never actually yields anything.
Actual behavior:
Compiles, and runs for that matter, without error, just silently yielding nothing.
I just spent a couple of hours tracking down a bug here, where return was accidentally used instead of yield. I'll admit I'm only starting to learn about generator functions and their uses, so it may be entirely possible that there are generator functions that want to return, rather than yield, in some cases, but my understanding is that every generator function should yield at some point.
Could we get a compiler warning for something like this? I'm not looking for a halt detector or anything :P just some kind of warning like "No 'yield' in reachable code," or something.
Accepting PRs for the rule: a generator function with a return statement must not have zero yield return statements (please make the error message less spec-like!)
#14043 has a valid example, a default implementation for a generator. the alternative would be to implement a function that returns the generator protocol, but that seems overly verbose.
Note: #30790 will address this only in cases where the return type is explicitly specified as Generator<T, void>, as returning the value above would not be assignable to void.
Activity
RyanCavanaugh commentedon Mar 14, 2017
Accepting PRs for the rule: a generator function with a
returnstatement must not have zeroyield returnstatements (please make the error message less spec-like!)mhegazy commentedon Mar 20, 2017
#14043 has a valid example, a default implementation for a generator. the alternative would be to implement a function that returns the generator protocol, but that seems overly verbose.
warn when generator function without any yield expression (microsoft#…
warn when generator function without any yield expression (microsoft#…
warn when generator function without any yield expression(microsoft#1…
warn when generator function without any yield expression(microsoft#1…
mhegazy commentedon Feb 20, 2018
In light of the discussion about errors vs warnings we should instead move this to tslint.
rbuckton commentedon Apr 6, 2019
Note: #30790 will address this only in cases where the return type is explicitly specified as
Generator<T, void>, as returning the value above would not be assignable tovoid.