509.0.0
This release is compatible with Swift 5.9.
Significant changes since the last release:
New rules
AlwaysUseLiteralForEmptyCollectionInit: transforms no-argument initializer calls on collection types to use the empty collection literal instead. For example,let x = [Int]()will be transformed intolet x: [Int] = []. This rule is opt-in (disabled by default).NoPlaygroundLiterals: emits lint findings when the playground literals (#colorLiteral,#fileLiteral,#imageLiteral) are used in code. Enabled by default.OmitExplicitReturns: removes unnecessaryreturnkeywords from single-expression function/closure/subscript/accessor bodies. This rule is opt-in (disabled by default).ReplaceForEachWithForLoop: emits lint findings when theforEachmethod is called with a closure literal at the end of a member access chain, indicating that it should be replaced by aforloop instead. Enabled by default.TypeNamesShouldBeCapitalized: emits lint findings when a type is declared with a name that is notUpperCamelCase. Enabled by default.
New configuration settings
multiElementCollectionTrailingCommas(boolean): When set tofalse, the last element of a multi-element array or dictionary literal will not have a trailing comma, even when the literal wraps across multiple lines. Defaults totrue(preserving the behavior of previous releases).
Bug fixes and behavior changes
- swift-format no longer crashes when formatting a
caseblock that contains no statements. - In multi-statement closures, there is now always a line break between the
inkeyword and the first statement. - Attributes before
importstatements are no longer wrapped. - The
NoParensAroundConditionsrule no longer removes parentheses around an immediately called closure. - The
NoAssignmentInExpressionsrule can be configured to ignore assignments that occur inside certain function calls. The default configuration ignores assignments insideXCTAssertNoThrow. - When an editor placeholder is found in the source, this is now treated as a warning instead of an error. This allows formatting to continue, treating the placeholder as a regular identifier.
- Keypath literals are properly wrapped and indented.
- Postfix-
#ifexpressions are no longer indented too far when they follow a closing parenthesis. - Indentation of multiline strings has been fixed in a number of locations.
- Documentation comment parsing has improved for rules like
BeginDocumentationCommentWithOneLineSummary,UseTripleSlashForDocumentationComments, andValidateDocumentationComments. - Diagnostic messages throughout swift-format have been cleaned up and improved.
- The
UseShorthandTypeNamesrule properly parenthesizes optionalsome/anytypes; for example,Optional<any P>becomes(any P)?, notany P?(which is invalid). - The
UseSynthesizedInitializerrule no longer warns that an initializer is redundant if it is declared with any attributes. - The lint/format plugins for SPM now default to processing all targets if the
--targetargument is not specified. - swift-format now emits a warning if you configure a rule that does not exist. This is meant to help catch typos in the configuration file.
- swift-format now does nothing if its input is empty (i.e., a zero-byte file). This suppresses a single trailing newline that would have otherwise been added in this case.
API changes
For developers using swift-format as a library, the types in the SwiftFormatConfiguration module have been folded into the SwiftFormat module. The SwiftFormat module is now the sole module you should import to use the linter/formatter APIs.
The SwiftFormatConfiguration module still exists to re-export the types for backwards compatibility, but this will be removed in the 510.0.0 release.