Skip to content
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

Merged
merged 2 commits into from
Aug 21, 2024

Conversation

calda
Copy link
Member

@calda calda commented Aug 13, 2024

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 like static func ==(lhs: T, rhs: T) -> Bool

// WRONG
static func == (_ lhs: MyView, _ rhs: MyView) -> Bool {
  lhs.id == rhs.id
}

// RIGHT
static func ==(_ lhs: MyView, _ rhs: MyView) -> Bool {
  lhs.id == rhs.id
}

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 of func ==(. 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:

// Looks strange, not allowed
static func isEqual (_ lhs: MyView, _ rhs: MyView) -> Bool {
  lhs.id == rhs.id
}

// Right
static func isEqual(_ lhs: MyView, _ rhs: MyView) -> Bool {
  lhs.id == rhs.id
}

@@ -34,6 +34,7 @@
--redundanttype inferred # redundantType, propertyType
--typeblanklines preserve # blankLinesAtStartOfScope, blankLinesAtEndOfScope
--emptybraces spaced # emptyBraces
--operatorfunc no-space # spaceAroundOperators
Copy link
Member Author

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'
@calda calda force-pushed the cal--infix-operator-definitions branch from 4fecc3f to c5d026e Compare August 13, 2024 18:29

<details>

```swift
// WRONG
let capacity = 1+2
let capacity = currentCapacity ?? 0
let mask = (UIAccessibilityTraitButton|UIAccessibilityTraitSelected)
Copy link
Member Author

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
Copy link
Member Author

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)
Copy link
Member Author

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).

@calda calda changed the title Update infix operator spacing rule to clarify rule for operator definitions like 'static func ==(lhs: T, rhs: T) -> Bool' Update infix operator spacing rule to clarify rule for operator definitions like static func ==(lhs: T, rhs: T) -> Bool Aug 13, 2024
@calda calda force-pushed the cal--infix-operator-definitions branch from 10d2ff5 to c5d026e Compare August 13, 2024 18:57
Copy link
Member Author

@calda calda left a 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 👍🏻

@calda calda merged commit 5a74d86 into master Aug 21, 2024
5 checks passed
@calda calda deleted the cal--infix-operator-definitions branch August 21, 2024 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant