Skip to content

Commit

Permalink
Don’t trigger shorthand_operator inside operator declaration (realm#4613
Browse files Browse the repository at this point in the history
)

Fixes realm#4611
  • Loading branch information
marcelofabri authored Dec 2, 2022
1 parent 7a8b2d1 commit 60128ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
[JP Simard](https://github.com/jpsim)
[#4558](https://github.com/realm/SwiftLint/issues/4558)

* Don't trigger `shorthand_operator` violations inside a shorthand operator
function declaration.
[Marcelo Fabri](https://github.com/marcelofabri)
[#4611](https://github.com/realm/SwiftLint/issues/4611)

#### Bug Fixes

* Fix false positives in `empty_enum_arguments` when the called expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ struct ShorthandOperatorRule: ConfigurationProviderRule, SwiftSyntaxRule {
Example("foo = self.foo \(operation) 1"),
Example("page = ceilf(currentOffset \(operation) pageWidth)"),
Example("foo = aMethod(foo \(operation) bar)"),
Example("foo = aMethod(bar \(operation) foo)")
Example("foo = aMethod(bar \(operation) foo)"),
Example("""
public func \(operation)= (lhs: inout Foo, rhs: Int) {
lhs = lhs \(operation) rhs
}
""")
]
} + [
Example("var helloWorld = \"world!\"\n helloWorld = \"Hello, \" + helloWorld"),
Expand Down Expand Up @@ -65,5 +70,26 @@ private extension ShorthandOperatorRule {

violations.append(node.leftOperand.positionAfterSkippingLeadingTrivia)
}

override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
if let binaryOperator = node.identifier.binaryOperator,
case let shorthandOperators = ShorthandOperatorRule.allOperators.map({ $0 + "=" }),
shorthandOperators.contains(binaryOperator) {
return .skipChildren
}

return .visitChildren
}
}
}

private extension TokenSyntax {
var binaryOperator: String? {
switch tokenKind {
case .spacedBinaryOperator(let str), .unspacedBinaryOperator(let str):
return str
default:
return nil
}
}
}

0 comments on commit 60128ab

Please sign in to comment.