Repository navigation
Require every if/else path to return in AlwaysReturnInFilter - #909
Merged
Merged
Conversation
AlwaysReturnInFilter treated only an if as conditional, so a return in an else or elseif counted as one that always runs. A callback that returned only from its else, or from an if/elseif chain with no else, passed even though one path returned nothing. An if chain now only counts as returning when it ends in an else and every branch returns. Other control structures, such as loops, switch and try, keep the previous leniency, where any return inside them counts.
An exit, die or throw anywhere in a filter callback's body turned the missing return error into the softer terminating statement warning, even when it sat in a nested closure or arrow function, such as a shutdown action. The callback itself never terminates there, so it still fails to return a value on that path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This follows on from #907 and fixes two remaining gaps in how
AlwaysReturnInFilterdecides whether a filter callback always returns a value.The sniff only treated
ifas conditional, so areturnin anelseorelseifcounted as one that always runs. A callback that returned only from itselse, or from anif/elseifchain without anelse, passed even though one path returned nothing:The sniff now follows each path through
if,elseifandelsechains, written with braces, aselse if, or with the alternative syntax. A chain only counts as returning when it ends in anelseand every branch returns, and that applies to chains nested in a branch too. Other control structures, such as loops,switchandtry, keep the previous leniency: areturnanywhere inside them counts. jrfnl raised the wider version of this on #520, which also covers checking every path through those structures. This PR deals withifchains only.The second commit fixes the check behind the terminating statement warning added for #719. An
exit,dieorthrowanywhere in the callback's body turned the missing return error into that softer warning, even when it sat in a nested closure or arrow function, such as ashutdownaction. The callback itself never terminates there, so this is now the error again.Both changes can report errors on code that passed before: callbacks with a path that returns nothing, and callbacks whose only
throworexitis in a nested closure. That is worth a line in the changelog. I ran the old and new sniff over the 21 Automattic plugins I have locally and the results did not change, but this sniff only reported four issues across them, so that is a small sample.