Treat empty yield as no-op for reachability #15699
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #15345.
We're getting slightly more than what we bargained for, i.e. not only we don't flag any statement as 'unreachable' in this canonical "empty generator":
but also here:
or here:
This is because it's simpler to add empty
yield
s tois_noop_for_reachability
, than to track state ("up to 1 empty yield").If there's anything more meaningful a function does, a different statement would be flagged as the first unreachable one, e.g.
Alternative
Another alternative is to pattern-match the entire function body as being
return; yield
. This would be slightly more code, and perhaps necessitate to marksuppress_unreachable_warnings
on the frame.I'm not entirely convinced
return; yield
should be special-cased on its own. Since the presence ofyield
is the only way to promote a function into a generator, we could be more lenient to emptyyield
s (other statements would still get flagged so it's unlikely that unreachable code would evade attention).