Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc93d1d

Browse files
committed
Fix false positive in redundant_set_access_control
Fixes realm#2182
1 parent 9a340a2 commit dc93d1d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Rules.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12506,6 +12506,12 @@ public var foo: Int
1250612506
var foo: Int
1250712507
```
1250812508

12509+
```swift
12510+
private final class A {
12511+
private(set) var value: Int
12512+
}
12513+
```
12514+
1250912515
</details>
1251012516
<details>
1251112517
<summary>Triggering Examples</summary>
@@ -12532,6 +12538,18 @@ open class Foo {
1253212538
}
1253312539
```
1253412540

12541+
```swift
12542+
class A {
12543+
↓internal(set) var value: Int
12544+
}
12545+
```
12546+
12547+
```swift
12548+
fileprivate class A {
12549+
↓fileprivate(set) var value: Int
12550+
}
12551+
```
12552+
1253512553
</details>
1253612554

1253712555

Source/SwiftLintFramework/Rules/RedundantSetAccessControlRule.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public struct RedundantSetAccessControlRule: ASTRule, ConfigurationProviderRule
1717
"private(set) public var foo: Int",
1818
"public let foo: Int",
1919
"public var foo: Int",
20-
"var foo: Int"
20+
"var foo: Int",
21+
"""
22+
private final class A {
23+
private(set) var value: Int
24+
}
25+
"""
2126
],
2227
triggeringExamples: [
2328
"↓private(set) private var foo: Int",
@@ -28,6 +33,16 @@ public struct RedundantSetAccessControlRule: ASTRule, ConfigurationProviderRule
2833
open class Foo {
2934
↓open(set) open var bar: Int
3035
}
36+
""",
37+
"""
38+
class A {
39+
↓internal(set) var value: Int
40+
}
41+
""",
42+
"""
43+
fileprivate class A {
44+
↓fileprivate(set) var value: Int
45+
}
3146
"""
3247
]
3348
)
@@ -47,6 +62,22 @@ public struct RedundantSetAccessControlRule: ASTRule, ConfigurationProviderRule
4762
return []
4863
}
4964

65+
let aclAttributes: Set<SwiftDeclarationAttributeKind> = [.private, .fileprivate, .internal, .public, .open]
66+
let explicitACL = dictionary.swiftAttributes.first { dict in
67+
guard let attribute = dict.attribute.flatMap(SwiftDeclarationAttributeKind.init) else {
68+
return false
69+
}
70+
71+
return aclAttributes.contains(attribute)
72+
}
73+
74+
// if it's an inferred `private`, it means the variable is actually inside a fileprivate structure
75+
if dictionary.accessibility.flatMap(AccessControlLevel.init(identifier:)) == .private,
76+
explicitACL?.offset == nil,
77+
dictionary.setterAccessibility.flatMap(AccessControlLevel.init(identifier:)) == .private {
78+
return []
79+
}
80+
5081
return [
5182
StyleViolation(ruleDescription: type(of: self).description,
5283
severity: configuration.severity,

0 commit comments

Comments
 (0)