SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

`opening_brace` on separate line should be permissible

Open lhunath opened this issue 2 years ago • 3 comments

New Issue Checklist

Describe the bug

opening_brace does not permit the brace to open on a separate line. However, this restriction can be excessive.

Some examples of cases where it might be very reasonable to open a block on its own line:

When a block takes another block as a return value:

destination: self.model.member.flatMap { member in
    { self.presenter?.detailsPresenter(for: member).view }
}

When a block is an option in a ternary:

destination: !slot.screens.isEmpty ?
    { self.router.screenshots(for: slot.screens, metrics: segment.metrics).featurePresenter } :
    { self.router.members(with: self.members.eraseToAnyChoice()).featurePresenter }

lhunath avatar Jul 14 '22 21:07 lhunath

@lhunath Did you consider formatting the ternary differently?

destination: !slot.screens.isEmpty
    ? { self.router.screenshots(for: slot.screens, metrics: segment.metrics).featurePresenter }
    : { self.router.members(with: self.members.eraseToAnyChoice()).featurePresenter } 

ondrejhanslik avatar Oct 30 '22 18:10 ondrejhanslik

perhaps, but I don't think forcing a different code style onto a project can be a solution.

lhunath avatar Dec 08 '22 00:12 lhunath

Adding another example to this, I made an array of closures formatted like this:

let predicates: [(SearchToken, SearchToken) -> Bool] = [ 
    { $0.searchText.starts(with: search) && !$1.searchText.starts(with: search) },
    { $0.scope != .bar && $1.scope == .bar },
    { $0.searchText.levenshteinDistance(to: search) < $1.searchText.levenshteinDistance(to: search) }
]

SwiftLint autofix puts everything on a single line, and then fails because the line length is too long.

jonduenas avatar Jan 21 '24 17:01 jonduenas