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

Ignore the second operands of binary left shift operations #5174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#5172](https://github.com/realm/SwiftLint/issues/5172)

* The `no_magic_numbers` rule will not trigger for bitwise shift
operations.
[Martin Redington](https://github.com/mildm8nnered)
[#5171](https://github.com/realm/SwiftLint/issues/5171)

## 0.52.4: Lid Switch

#### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ struct NoMagicNumbersRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule
let a = Int(3)
}
class MyTest: XCTestCase {}
""")
"""),
Example("let foo = 1 << 2"),
Example("let foo = 1 >> 2"),
Example("let foo = 2 >> 2"),
Example("let foo = 2 << 2")
SimplyDanny marked this conversation as resolved.
Show resolved Hide resolved
],
triggeringExamples: [
Example("foo(↓321)"),
Expand Down Expand Up @@ -131,6 +135,9 @@ private extension NoMagicNumbersRule {
if node.isMemberOfATestClass(testParentClasses) {
return
}
if node.isOperandOfBitwiseShiftOperation() {
return
}
let violation = node.positionAfterSkippingLeadingTrivia
if let extendedTypeName = node.extendedTypeName() {
if !testClasses.contains(extendedTypeName) {
Expand Down Expand Up @@ -196,4 +203,20 @@ private extension ExprSyntaxProtocol {
}
return nil
}

func isOperandOfBitwiseShiftOperation() -> Bool {
guard
let siblings = parent?.as(ExprListSyntax.self)?.children(viewMode: .sourceAccurate),
siblings.count == 3
else {
return false
}

let operatorIndex = siblings.index(after: siblings.startIndex)
if let tokenKind = siblings[operatorIndex].as(BinaryOperatorExprSyntax.self)?.operatorToken.tokenKind {
return tokenKind == .binaryOperator("<<") || tokenKind == .binaryOperator(">>")
}

return false
}
}