-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Add option for unneeded_override
to affect initializers
#5270
Add option for unneeded_override
to affect initializers
#5270
Conversation
Generated by 🚫 Danger |
c3fb77e
to
013045c
Compare
unneeded_override
affect initializersunneeded_override
to affect initializers
Any thoughts on this? |
534edef
to
cf90161
Compare
@SimplyDanny Could you review this enhancement? This would be my first contribution here and any suggestion are welcome. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very well done! Not much to say. 👍
To check the rule against a few OSS project, please set the new option to true
by default. With the report produced by the build we are able to judge on the option's impact. Typically we find some special cases that need to be considered in the implementation. After the report is checked, you can turn off the option again.
Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Outdated
Show resolved
Hide resolved
cf90161
to
c02861a
Compare
Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Outdated
Show resolved
Hide resolved
Thank you for reviewing this!
I made the change and checked the report. It seems like the differences found were the expected. Cases with There are also the
I was going for off by default, but now I'm thinking that it would be expected for this to be on by default... |
override init!(arg: ...) {
super.init(arg: arg)
} and override init?(arg: ...) {
super.init(arg: arg)
} should not trigger. It's allowed to override a failable initializer with an initializer that's implicitly unwrapped and vice versa. The private override init() {
super.init()
} is an indicator that this override exists to hide the initializer. If the parent was
The thing is that folks might already have adopted the rule and it's always surprising if the same rule suddenly triggers on more cases. Sure, then they can set the option to |
Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Outdated
Show resolved
Hide resolved
c02861a
to
65790cd
Compare
Thanks for the insights on failable, implicitly unwrapped and private initializers, I'm fixing those. I found a solution to not expose the private protocol contents while conforming the types to it: class Foo {}
private protocol Bar {
var value: String { get }
}
private extension Foo {
var value: String { "bar" }
}
extension Foo: Bar {} I'm thinking this should be good for us here, let me know if you judge otherwise. |
It's good enough in the sense of a workaround. But is it really easy to follow? If it doesn't bother you too much, I'd still prefer the extra |
65790cd
to
f186e06
Compare
Interesting point, I agree... I just changed it and added the checks for failable, implicitly unwrapped and private initializers. |
Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/UnneededOverrideRuleConfiguration.swift
Outdated
Show resolved
Hide resolved
a2ae91e
to
4ed6b6d
Compare
Head branch was pushed to by a user without write access
4ed6b6d
to
5b2ae94
Compare
Thanks, @leonardosrodrigues0! That's a great first contribution. 👍 |
Thank you @SimplyDanny for the reviews and the support!! |
Changes
These changes add an
affect_initializers
option tounneeded_override
rule . Closes #5265.When the option is enabled, the overridden initializers are checked in the same way methods are. In addition, failable (
init?
) and implicitly unwrapped (init!
) initializers never trigger the rule, as it is allowed to override one with the other and vice versa. Finally,private
overrides also don't trigger the rule, as they can be used to hide thesuper
initializer.Implementation
A new
OverridableDecl
protocol was built to allow bothFunctionDeclSyntax
andInitializerDeclSyntax
to be checked using the same function. The check forinit
name on the member access expression was implemented by comparing directly to the"init"
string, as it is done inExplicitInitRule
,NSNumberInitAsFunctionReferenceRule
andReduceIntoRule
.