Skip to content

Commit

Permalink
Remove trailing comma when Sendable was last (#5955)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Jan 13, 2025
1 parent f045130 commit 3f2b86c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#### Bug Fixes

* None.
* Fix `redundant_sendable` correction by removing a remaining trailing comma as well when `Sendable` was last.
[SimplyDanny](https://github.com/SimplyDanny)
[#5952](https://github.com/realm/SwiftLint/issues/5952)

## 0.58.0: New Year’s Fresh Fold

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ struct RedundantSendableRule: Rule {
corrections: [
Example("@MainActor struct S: Sendable {}"):
Example("@MainActor struct S {}"),
Example("actor A: Sendable {}"):
Example("actor A {}"),
Example("actor A: Sendable /* trailing comment */{}"):
Example("actor A /* trailing comment */{}"),
Example("@MyActor enum E: Sendable { case a }", configuration: ["global_actors": ["MyActor"]]):
Example("@MyActor enum E { case a }"),
Example("actor A: B, Sendable, C {}"):
Example("actor A: B, C {}"),
Example("""
actor A: B, Sendable, C // comment
{}
"""):
Example("""
actor A: B, C // comment
{}
"""),
Example("@MainActor protocol P: A, Sendable {}"):
Example("@MainActor protocol P: A {}"),
]
)
}
Expand Down Expand Up @@ -114,17 +122,22 @@ private extension DeclGroupSyntax where Self: NamedDeclSyntax {
return self
}
let inheritedTypes = inheritanceClause.inheritedTypes.filter { !$0.isSendable }
if inheritedTypes.isEmpty {
return with(\.inheritanceClause, nil)
.with(\.name.trailingTrivia, inheritanceClause.leadingTrivia + inheritanceClause.trailingTrivia)
if let lastType = inheritedTypes.last, let lastIndex = inheritedTypes.index(of: lastType) {
return with(\.inheritanceClause, inheritanceClause
.with(\.inheritedTypes, inheritedTypes.with(\.[lastIndex], lastType.withoutComma)))
}
return with(\.inheritanceClause, inheritanceClause
.with(\.inheritedTypes, inheritedTypes))
return with(\.inheritanceClause, nil)
.with(\.name.trailingTrivia, inheritanceClause.leadingTrivia + inheritanceClause.trailingTrivia)
}
}

private extension InheritedTypeSyntax {
var isSendable: Bool {
type.as(IdentifierTypeSyntax.self)?.name.text == "Sendable"
}

var withoutComma: InheritedTypeSyntax {
with(\.trailingComma, nil)
.with(\.trailingTrivia, trailingTrivia)
}
}

0 comments on commit 3f2b86c

Please sign in to comment.