-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Rule Request: Prefer static
over final class
#5471
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
Comments
The rule conflicts with nested classes. For example: extension A {
final class B { }
} This obviously cannot be replaced with the |
Which part is replaced by the rule in this example? |
Sorry, the previous example was incorrect. The following code is triggered by the rule, while it should not, I believe: final class A: UIView {
override class var layerClass: AnyClass {
CALayer.self
}
} Seems like it happens because of combination of a |
Just checked with class B {
class var a: Int { 1 }
}
final class A: B {
override static var a: Int { 2 }
} which compiles fine in Swift 5.10. |
This compiles fine, you are right. But the following does not. final class A: B {
override class var a: Int { 2 }
} This is why I highlighted the default Xcode suggestion behavior in this case. And, by the way, should the rule even been triggered by this kind of code? It has no sequential combination of |
This is compiles fine, you are right. But the following does not.
What is the error in this example? Both variants seem to be fine for me.
This is exactly the idea of this rule. It mimics the behavior of the compiler when you use |
So let me clarify a little. Let's say there is the same base class class B {
class var a: Int { 1 }
}
final class A: B {
override class var a: Int { 2 }
}
final class A: B {
override static var a: Int { 2 }
}
class A: B {
override class var a: Int { 2 }
} class A: B {
override static var a: Int { 2 }
} Is this the intended behavior of the rule? |
Yes, once a class comes with |
Ok, thanks! |
The modifiers
final
andclass
on a method/property have the same meaning asstatic
. The rule should flag appearances offinal class
on the same declaration and replace them bystatic
.I don't think there is any need for configuration. The rule could be enabled by default, but this can be decided in review when browsing through the reported OSS findings.
Triggering:
Non-triggering:
The text was updated successfully, but these errors were encountered: