Repository navigation
Add check_if_and_switch_expressions option to implicit_return - #6950
Open
andrewse02 wants to merge 2 commits into
Open
andrewse02 wants to merge 2 commits into
andrewse02 wants to merge 2 commits into
Conversation
When enabled, the rule also reports `return` statements in all branches of an `if` or `switch` that is the only statement of a body, as long as removing them turns it into a valid `if`/`switch` expression. Nested `if`/`switch` statements in branches are handled recursively. The option defaults to `false`. Resolves realm#6167. Co-authored-by: Nandhini Subramani <42835327+nandhinisubbu@users.noreply.github.com>
Without a contextual type, the branches of an `if` or `switch` expression are type-checked independently. Removing the `return` statements can therefore break code that compiled before, e.g. branches returning `Optional(1)` and `nil` in an untyped closure.
andrewse02
marked this pull request as ready for review
October 9, 2026 19:49
Generated by 🚫 Danger |
This branch has not been deployed
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.
Resolves #6167. Picks up #6310 by @nandhinisubbu (credited as co-author), addressing the review feedback there:
ifinsideswitchcases and vice versa, pluselse ifchains.CodeBlockItemListSyntax/IfExprSyntax/SwitchExprSyntax, so the visitor stays small.check_if_and_switch_expressions(defaultfalse). SwiftLint's own config doesn't change, so adopting the option in this repo can come in a follow-up.It only triggers when every branch is a single
returnwith a value, so removing them yields a validif/switchexpression. It doesn't trigger for anifwithoutelse, barereturns, a branch that ends inthrow, aswitchcontaining#if, or a labeledswitch.Closures are only checked when they declare an explicit return type. Without a contextual type, the branches of an
if/switchexpression are type-checked independently, so removing thereturns can break code that compiled before (e.g. branches returningOptional(1)andnilin an untyped closure).