Add redundant_final_actor opt-in rule#6504
Add redundant_final_actor opt-in rule#6504William-Laverty wants to merge 3 commits intorealm:mainfrom
redundant_final_actor opt-in rule#6504Conversation
Here's an example of your CHANGELOG entry: * Add `redundant_final_actor` opt-in rule.
[William-Laverty](https://github.com/William-Laverty)
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)note: There are two invisible spaces after the entry's text. Generated by 🚫 Danger |
Actors in Swift cannot be subclassed (SE-0306), making the `final`
modifier redundant on actor declarations. This new opt-in rule detects
and auto-corrects `final actor` to just `actor`.
Examples:
- `final actor MyActor {}` → `actor MyActor {}`
- `public final actor DataStore {}` → `public actor DataStore {}`
Closes realm#6407
- Use @SwiftSyntaxRule(explicitRewriter: true, optIn: true) instead of directly conforming to OptInRule, which caused a type mismatch - Rebase on latest upstream main - Re-run rules and reporters register
2d01a01 to
f655ffa
Compare
SimplyDanny
left a comment
There was a problem hiding this comment.
Good idea!
Have in mind, however, that actors don't support inheritance now. That is open to change in the future. We should perhaps mention that in a rational. RuleDescription support a rational value we can use to explain that a little and consider it a caveat.
What about final methods and properties?
actor A {
final func f() {}
}| final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
| override func visitPost(_ node: ActorDeclSyntax) { | ||
| if let finalModifier = node.modifiers.first(where: { $0.name.text == "final" }) { | ||
| violations.append(finalModifier.positionAfterSkippingLeadingTrivia) |
There was a problem hiding this comment.
You can append violations objects here which can contain a correction. It's a simple range-replace-based correction, but should be sufficient for this use case. If something is possible at all, it is often much easier than an explicit rewriter.
Address SimplyDanny's suggestion to use range-replace correction appended to the violation, removing the need for a separate Rewriter.
Summary
Implements #6407.
Actors in Swift cannot be subclassed (SE-0306), so the
finalmodifier is always redundant on actor declarations. The compiler doesn't warn about this, but it's unnecessary noise similar to redundantinternalaccess control.New Rule:
redundant_final_actorfinalmodifierExamples
Changes
RedundantFinalActorRule.swiftwith visitor + rewriterBuiltInRules.swift