Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected redundantReturn behavior with some View functions #1819

Closed
okalentiev opened this issue Aug 16, 2024 · 4 comments
Closed

Unexpected redundantReturn behavior with some View functions #1819

okalentiev opened this issue Aug 16, 2024 · 4 comments
Labels
bug fixed in develop bug/feature resolved in the develop branch

Comments

@okalentiev
Copy link

Swift compiler cannot infer return type without explicit return.

extension View {
    public func iOS16_scrollIndicatorsHidden() -> some View {
        if #available(iOS 16.0, *) {
            return self.scrollIndicators(.hidden)
        } else {
            return self
        }
    }
}

SwiftFormat removes returns from the above code, which makes compilation break because of the following error:

Branches have mismatching types 'some View' and 'Self'

Looks like an additional use-case to #1751 and #1749

Setup:

  • SwiftFormat version: 0.54.3
  • Both conditionalAssignment and redundantReturn are enabled
  • Swift version 5.9
@nicklockwood
Copy link
Owner

@calda any thoughts on how we can detect this scenario?

@nicklockwood
Copy link
Owner

Looks like an additional use-case to #1751 and #1749

I think these are actually all different scenarios, although they seem related

@calda
Copy link
Collaborator

calda commented Aug 26, 2024

This is a weird one. I guess it's because an if/switch expression has to return the same result type on each branch? In normal functions this is already enforced by the function having a single result type, but that's not the case for a function with an opaque result type.

I think the fix would be to fixable the redundantReturn if/switch expression functionality within functions that return a some opaque result type.

@nicklockwood nicklockwood added the fixed in develop bug/feature resolved in the develop branch label Sep 1, 2024
@nicklockwood
Copy link
Owner

@okalentiev fixed in 0.54.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fixed in develop bug/feature resolved in the develop branch
Projects
None yet
Development

No branches or pull requests

3 participants