Don’t trigger shorthand_operator inside operator declaration (#4613)
Fixes #4611
This commit is contained in:
parent
7a8b2d1dab
commit
60128ab196
|
@ -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
|
||||
|
|
|
@ -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"),
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue