realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.64k stars 2.22k forks source link

operator_usage_whitespace treats `>` in generic specialization as operator when initializing #1897

Closed rastersize closed 6 years ago

rastersize commented 7 years ago

New Issue Checklist

Bug Report

The operator_usage_whitespace rule matches the trailing > when specializing a generic type and initializing it at the same time.

Environment

# insert yaml contents here
$ echo "internal let foo = GenericType<(UIViewController) -> Void>()" | swiftlint lint --no-cache --use-stdin --enable-all-rules
<nopath>:1:58: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
Done linting! Found 1 violation, 0 serious in 1 file.
// This triggers a violation:
let foo = GenericType<(UIViewController) -> Void>()
// I.e. a generic type that’s typed with a closure when initialized.
// It’s autocorrected to:
let foo = GenericType<(UIViewController) -> Void > ()
rastersize commented 7 years ago

The issue can be worked around in two ways. Neither of the following examples trigger the bug:

let foo: GenericType<(UIViewController) -> Void> = .init()

or

typealias SomeClosure = (UIViewController) -> Void
let foo = GenericType<SomeClosure>()

I went with the latter since it seems cleaner to me. Also SwiftFormat gets tripped up by the problem as well as the first workaround.

macdrevx commented 6 years ago

Also happens in situations where a generic parameter itself has a generic parameter:

let foo = Foo<Bar<T>, Baz>()
// is autocorrected to
let foo = Foo<Bar<T>, Baz > ()
marcelofabri commented 6 years ago

Fixed in https://github.com/realm/SwiftLint/pull/2196

lazarevzubov commented 2 years ago

There's still an issue with generic parameters, if they're placed on different lines. This:

let something = Something<GenericParameter1,
                          GenericParameter2>()

Becomes this:

let something = Something < GenericParameter1,
                          GenericParameter2 > ()
SimplyDanny commented 2 years ago

Would you please open a separate issue for that, @lazarevzubov? Herein, it will probably get lost.

lazarevzubov commented 2 years ago

Would you please open a separate issue for that, @lazarevzubov? Herein, it will probably get lost.

Sure, done: https://github.com/realm/SwiftLint/issues/3960