Skip to content

Commit 4b9208a

Browse files
authored
Ignore various assignment operators in void_function_in_ternary (#6133)
1 parent 388d452 commit 4b9208a

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
[imsonalbajaj](https://github.com/imsonalbajaj)
2323
[#6054](https://github.com/realm/SwiftLint/issues/6054)
2424

25+
* Ignore various assignment operators like `=`, `+=`, `&=`, etc. with right-hand side
26+
ternary expressions otherwise violating the `void_function_in_ternary` rule.
27+
[SimplyDanny](https://github.com/SimplyDanny)
28+
[#5611](https://github.com/realm/SwiftLint/issues/5611)
29+
2530
* Rewrite the following rules with SwiftSyntax:
2631
* `accessibility_label_for_image`
2732
* `accessibility_trait_for_button`

Source/SwiftLintBuiltInRules/Rules/Idiomatic/VoidFunctionInTernaryConditionRule.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ struct VoidFunctionInTernaryConditionRule: Rule {
1111
kind: .idiomatic,
1212
minSwiftVersion: .fiveDotOne,
1313
nonTriggeringExamples: [
14-
Example("let result = success ? foo() : bar()"),
1514
Example("""
1615
if success {
1716
askQuestion()
@@ -60,6 +59,14 @@ struct VoidFunctionInTernaryConditionRule: Rule {
6059
subscript(index: Int) -> Int {
6160
index == 0 ? defaultValue() : compute(index)
6261
"""),
62+
Example("""
63+
var a = b ? c() : d()
64+
a += b ? c() : d()
65+
a -= b ? c() : d()
66+
a *= b ? c() : d()
67+
a &<<= b ? c() : d()
68+
a &-= b ? c() : d()
69+
"""),
6370
],
6471
triggeringExamples: [
6572
Example("success ↓? askQuestion() : exit()"),
@@ -144,18 +151,43 @@ private extension VoidFunctionInTernaryConditionRule {
144151

145152
private extension ExprListSyntax {
146153
var containsAssignment: Bool {
147-
children(viewMode: .sourceAccurate).contains(where: { $0.is(AssignmentExprSyntax.self) })
154+
children(viewMode: .sourceAccurate).contains {
155+
if let binOp = $0.as(BinaryOperatorExprSyntax.self) {
156+
// https://developer.apple.com/documentation/swift/operator-declarations
157+
return [
158+
"*=",
159+
"/=",
160+
"%=",
161+
"+=",
162+
"-=",
163+
"<<=",
164+
">>=",
165+
"&=",
166+
"|=",
167+
"^=",
168+
"&*=",
169+
"&+=",
170+
"&-=",
171+
"&<<=",
172+
"&>>=",
173+
".&=",
174+
".|=",
175+
".^=",
176+
].contains(binOp.operator.text)
177+
}
178+
return $0.is(AssignmentExprSyntax.self)
179+
}
148180
}
149181
}
150182

151183
private extension CodeBlockItemSyntax {
152184
var isImplicitReturn: Bool {
153-
isClosureImplictReturn || isFunctionImplicitReturn ||
185+
isClosureImplicitReturn || isFunctionImplicitReturn ||
154186
isVariableImplicitReturn || isSubscriptImplicitReturn ||
155-
isAcessorImplicitReturn
187+
isAccessorImplicitReturn
156188
}
157189

158-
var isClosureImplictReturn: Bool {
190+
var isClosureImplicitReturn: Bool {
159191
guard let parent = parent?.as(CodeBlockItemListSyntax.self),
160192
let grandparent = parent.parent else {
161193
return false
@@ -191,7 +223,7 @@ private extension CodeBlockItemSyntax {
191223
return parent.children(viewMode: .sourceAccurate).count == 1 && subscriptDecl.allowsImplicitReturns
192224
}
193225

194-
var isAcessorImplicitReturn: Bool {
226+
var isAccessorImplicitReturn: Bool {
195227
guard let parent = parent?.as(CodeBlockItemListSyntax.self),
196228
parent.parent?.parent?.as(AccessorDeclSyntax.self) != nil else {
197229
return false

0 commit comments

Comments
 (0)