Skip to content
Open
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 @@ -57,6 +57,11 @@
increases when linting a large number of files.
[ephraimkunz](https://github.com/ephraimkunz)

* Stop `redundant_final` from reporting `final class` members of final classes,
which made it undo the `non_overridable_class_declaration` rule's correction.
[nu11waredev](https://github.com/nu11waredev)
[#6936](https://github.com/realm/SwiftLint/issues/6936)

## 0.65.1: Fresh Folded Fixtures

### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct RedundantFinalRule: Rule {
and their members. Note that this may change in future Swift versions if actor inheritance is introduced.

Additionally, `final` is redundant on members of a `final class` since they cannot be overridden.
Type members declared as `final class` are not reported. For them, `final class` means the same as
`static`.
""",
kind: .idiomatic,
nonTriggeringExamples: #examples([
Expand All @@ -37,6 +39,13 @@ struct RedundantFinalRule: Rule {
final func doWork() {}
}
""",
"""
final class MyClass {
final class var value: Int { 0 }
final class func doWork() {}
final class subscript(index: Int) -> Int { index }
}
""",
]),
triggeringExamples: #examples([
"↓final actor MyActor {}",
Expand Down Expand Up @@ -66,6 +75,12 @@ struct RedundantFinalRule: Rule {
}
}
""",
"""
final class MyClass {
↓final func doWork() {}
final class func doMoreWork() {}
}
""",
]),
corrections: #corrections([
"final actor MyActor {}":
Expand Down Expand Up @@ -133,19 +148,21 @@ private extension RedundantFinalRule {
}

override func visitPost(_ node: FunctionDeclSyntax) {
if finalTypeStack.peek() == true, let finalModifier = node.modifiers.modifier(with: .final) {
addViolation(for: finalModifier)
}
collectViolation(for: node.modifiers)
}

override func visitPost(_ node: VariableDeclSyntax) {
if finalTypeStack.peek() == true, let finalModifier = node.modifiers.modifier(with: .final) {
addViolation(for: finalModifier)
}
collectViolation(for: node.modifiers)
}

override func visitPost(_ node: SubscriptDeclSyntax) {
if finalTypeStack.peek() == true, let finalModifier = node.modifiers.modifier(with: .final) {
collectViolation(for: node.modifiers)
}

private func collectViolation(for modifiers: DeclModifierListSyntax) {
// `final class` members are equivalent to `static` ones. Removing `final` would leave a `class` member.
if finalTypeStack.peek() == true, !modifiers.contains(keyword: .class),
let finalModifier = modifiers.modifier(with: .final) {
addViolation(for: finalModifier)
}
}
Expand Down
Loading