nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

`redundantReturn` has no effect on computed property `get { }` block #1249

Closed orchetect closed 2 years ago

orchetect commented 2 years ago

SwiftFormat 0.49.14 Xcode 14 beta 5

With SwiftFormat reset to default settings (which has redundantReturn enabled), the following code is left intact as-is.

var foo: Int {
    get {
        return 1
    }
    set {
        print("bar")
    }
}

The expectation would be that the get { } block would be simplified to:

var foo: Int {
    get {
        1
    }
    set {
        print("bar")
    }
}
nicklockwood commented 2 years ago

Have you set the --swiftversion? It must be set to 5.1 or higher for this rule to be enabled.

orchetect commented 2 years ago

Ah you are correct. Swift version is set to auto.

Which raises another question: when invoked within Xcode using the source editor plugin, does it have any way to ascertain Swift version that is used in the current file? I'm not sure if that kind of metadata is available for source editor plugins.

I would assume the canonical way to solve this is to have .swiftformat files set up in every project and define the Swift version within it. Then have the Xcode plugin set to Infer Options Automatically.

nicklockwood commented 2 years ago

Which raises another question: when invoked within Xcode using the source editor plugin, does it have any way to ascertain Swift version that is used in the current file? I'm not sure if that kind of metadata is available for source editor plugins.

It doesn't have any way to do this, and Xcode doesn't expose it. I might be able to infer it based on certain heuristics, but I don't attempt to do that currently.

I would assume the canonical way to solve this is to have .swiftformat files set up in every project and define the Swift version within it. Then have the Xcode plugin set to Infer Options Automatically.

That's generally how it works, however the plugin can't read the .swiftformat file due to sandboxing restrictions, so it has to be configured separately (there is a neat workaround for this though using XPC that will hopefully land in the next major release).

orchetect commented 2 years ago

Thank you for the clarifications. ❤️