Don’t trigger shorthand_operator inside operator declaration (#4613)

Fixes #4611
This commit is contained in:
Marcelo Fabri 2022-12-01 23:00:44 -08:00 committed by GitHub
parent 7a8b2d1dab
commit 60128ab196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -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

View File

@ -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
}
}
}