-
Notifications
You must be signed in to change notification settings - Fork 323
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
Update infix operator spacing rule to clarify rule for operator definitions like static func ==(lhs: T, rhs: T) -> Bool
#286
Conversation
@@ -34,6 +34,7 @@ | |||
--redundanttype inferred # redundantType, propertyType | |||
--typeblanklines preserve # blankLinesAtStartOfScope, blankLinesAtEndOfScope | |||
--emptybraces spaced # emptyBraces | |||
--operatorfunc no-space # spaceAroundOperators |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has no effect for now, but will kick in after we enable the spaceAroundOperators
rule in a follow-up
…itions like 'static func ==(lhs: T, rhs: T) -> Bool'
4fecc3f
to
c5d026e
Compare
|
||
<details> | ||
|
||
```swift | ||
// WRONG | ||
let capacity = 1+2 | ||
let capacity = currentCapacity ?? 0 | ||
let mask = (UIAccessibilityTraitButton|UIAccessibilityTraitSelected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this example since it was outdated
|
||
<details> | ||
|
||
```swift | ||
// WRONG | ||
let capacity = 1+2 | ||
let capacity = currentCapacity ?? 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tweaked this example since consecutive spaces like this are already disallowed by another rule
README.md
Outdated
@@ -2450,24 +2450,34 @@ _You can enable the following settings in Xcode by running [this script](resourc | |||
|
|||
### Operators | |||
|
|||
* <a id='infix-operator-spacing'></a>(<a href='#infix-operator-spacing'>link</a>) **Infix operators should have a single space on either side.** Prefer parenthesis to visually group statements with many operators rather than varying widths of whitespace. This rule does not apply to range operators (e.g. `1...3`) and postfix or prefix operators (e.g. `guest?` or `-1`). [![SwiftLint: operator_usage_whitespace](https://img.shields.io/badge/SwiftLint-operator__usage__whitespace-007A87.svg)](https://realm.github.io/SwiftLint/operator_usage_whitespace) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the "Prefer parenthesis" part because it feels separate and doesn't include autocorrect support, and removed the comment on postfix / prefix operators because it's redundant (the rule explicitly only applies to infix operators).
static func ==(lhs: T, rhs: T) -> Bool
10d2ff5
to
c5d026e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strong support for this change internally 👍🏻
Summary
This PR proposals an update to the infix operator spacing rule, clarifying that we should omit the trailing space after operators in operator
func
implementations likestatic func ==(lhs: T, rhs: T) -> Bool
Reasoning
The existing wording of the rule implies that it applies in all cases. However, for operator definitions like this, omitting the space is more common.
In our codebase we have 540 examples of
func == (
and 709 examples offunc ==(
. All of the other custom operator definitions I found for other operators (+
,-
,+=
,!=
,<
,>
) also omitted the trailing space.This is also more consistent with other function definitions. For example, adding a space here looks weird and is disallowed in all other function definitions: