Skip to content

Add check_if_and_switch_expressions option to implicit_return - #6950

Open
andrewse02 wants to merge 2 commits into
realm:mainfrom
andrewse02:implicit-return-conditional-expressions
Open

andrewse02 wants to merge 2 commits into
realm:mainfrom
andrewse02:implicit-return-conditional-expressions

Conversation

@andrewse02

@andrewse02 andrewse02 commented Oct 9, 2026 •

Copy link
Copy Markdown

Resolves #6167. Picks up #6310 by @nandhinisubbu (credited as co-author), addressing the review feedback there:

  • Handles nesting: if inside switch cases and vice versa, plus else if chains.
  • The checks live in computed properties on CodeBlockItemListSyntax/IfExprSyntax/SwitchExprSyntax, so the visitor stays small.
  • Opt-in through check_if_and_switch_expressions (default false). 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 return with a value, so removing them yields a valid if/switch expression. It doesn't trigger for an if without else, bare returns, a branch that ends in throw, a switch containing #if, or a labeled switch.

Closures are only checked when they declare an explicit return type. Without a contextual type, the branches of an if/switch expression are type-checked independently, so removing the returns can break code that compiled before (e.g. branches returning Optional(1) and nil in an untyped closure).

andrewse02 and others added 2 commits October 9, 2026 09:58
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.
@SwiftLintBot

Copy link
Copy Markdown
20 Messages
📖 Building this branch resulted in a binary size of 28791.07 KiB vs 28755.54 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.67 s vs 0.68 s on main (1% faster).
📖 Linting Alamofire with this PR took 0.98 s vs 0.98 s on main (0% slower).
📖 Linting Brave with this PR took 5.48 s vs 5.51 s on main (0% faster).
📖 Linting Brigade with this PR took 18.09 s vs 18.21 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 33.06 s vs 32.75 s on main (0% slower).
📖 Linting Firefox with this PR took 11.78 s vs 11.78 s on main (0% slower).
📖 Linting Kickstarter with this PR took 7.4 s vs 7.38 s on main (0% slower).
📖 Linting Moya with this PR took 0.38 s vs 0.38 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 2.57 s vs 2.59 s on main (0% faster).
📖 Linting Nimble with this PR took 0.56 s vs 0.56 s on main (0% slower).
📖 Linting PocketCasts with this PR took 7.43 s vs 7.44 s on main (0% faster).
📖 Linting Quick with this PR took 0.35 s vs 0.36 s on main (2% faster).
📖 Linting Realm with this PR took 2.94 s vs 3.0 s on main (2% faster).
📖 Linting Sourcery with this PR took 1.63 s vs 1.65 s on main (1% faster).
📖 Linting Swift with this PR took 4.61 s vs 4.61 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.18 s vs 0.18 s on main (0% slower).
📖 Linting VLC with this PR took 1.45 s vs 1.44 s on main (0% slower).
📖 Linting Wire with this PR took 16.55 s vs 16.6 s on main (0% faster).
📖 Linting WordPress with this PR took 10.66 s vs 10.66 s on main (0% slower).

Generated by 🚫 Danger

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implicit_return doesn't trigger for if/switch expressions

2 participants