You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Rule Request: Prefer static over final class #5471
The modifiers final and class on a method/property have the same meaning as static. The rule should flag appearances of final class on the same declaration and replace them by static.
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.
Seems like it happens because of combination of a final class and a class property override. Swift do allow replacing class keyword with static in this scenario, but Xcode suggests using override class var by default.
Seems like it happens because of combination of a final class and a class property override. Swift do allow replacing class keyword with static in this scenario, but Xcode suggests using override class var by default.
Seems like it happens because of combination of a final class and a class property override. Swift do allow replacing class keyword with static in this scenario, but Xcode suggests using override class var by default.
This compiles fine, you are right. But the following does not.
finalclassA:B{overrideclassvara: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 final class keywords inside the class. It does have a separate final class and a separate class / static property (not class or func) instead, isn't it?
This is compiles fine, you are right. But the following does not.
finalclassA:B{overrideclassvara:Int{2}}
This is why I highlighted the default Xcode suggestion behavior in this case.
What is the error in this example? Both variants seem to be fine for me.
And, by the way, should the rule even been triggered by this kind of code? It has no sequential combination of final class keywords inside the class. It does have a separate final class and a separate class / static property (not class or func) instead, isn't it?
This is exactly the idea of this rule. It mimics the behavior of the compiler when you use open in a final class. Using class instead of static in a final class is the same "violation" but for class properties or functions. The purpose of this rule is to serve the same warning in the "static" case.
This is exactly the idea of this rule. It mimics the behavior of the compiler when you use open in a final class. Using class instead of static in a final class is the same "violation" but for class properties or functions. The purpose of this rule is to serve the same warning in the "static" case.
So let me clarify a little.
Let's say there is the same base class B.
classB{classvara:Int{1}}
The following code will FAIL to pass the rule, because the final classA should not contain a class property, and it should be replaced with a static one:
finalclassA:B{overrideclassvara:Int{2}}
Thus the following code will PASS the rule as expected:
finalclassA:B{overridestaticvara:Int{2}}
Both of the following blocks of code will PASS the rule, because the class A is not marked as final, so it can contain any property you want, class or static:
Yes, once a class comes with final you should not state that something can be overridden by using class. Without the final it's your decision to put static or class.
Activity
omelics commentedon May 16, 2024
The rule conflicts with nested classes. For example:
This obviously cannot be replaced with the
statickeyword and will lead to a compilation error.SimplyDanny commentedon May 16, 2024
Which part is replaced by the rule in this example?
omelics commentedon May 16, 2024
Sorry, the previous example was incorrect.
The following code is triggered by the rule, while it should not, I believe:
Seems like it happens because of combination of a
final classand aclassproperty override. Swift do allow replacingclasskeyword withstaticin this scenario, but Xcode suggests usingoverride class varby default.SimplyDanny commentedon May 16, 2024
Just checked with
which compiles fine in Swift 5.10.
omelics commentedon May 16, 2024
This compiles fine, you are right. But the following does not.
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
final classkeywords inside the class. It does have a separatefinal classand a separateclass/staticproperty (notclassorfunc) instead, isn't it?SimplyDanny commentedon May 16, 2024
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
openin afinalclass. Usingclassinstead ofstaticin afinalclass is the same "violation" but for class properties or functions. The purpose of this rule is to serve the same warning in the "static" case.omelics commentedon May 16, 2024
So let me clarify a little.
Let's say there is the same base class
B.final classAshould not contain aclassproperty, and it should be replaced with astaticone:Ais not marked asfinal, so it can contain any property you want,classorstatic:Is this the intended behavior of the rule?
SimplyDanny commentedon May 16, 2024
Yes, once a class comes with
finalyou should not state that something can be overridden by usingclass. Without thefinalit's your decision to putstaticorclass.omelics commentedon May 16, 2024
Ok, thanks!