-
Notifications
You must be signed in to change notification settings - Fork 638
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 preferInferredTypes rule #1640
Add preferInferredTypes rule #1640
Conversation
let prevToken = formatter.token(at: prevIndex), case let .identifier(name) = prevToken, | ||
let firstChar = name.first, firstChar != "$", | ||
let prevToken = formatter.token(at: prevIndex), | ||
formatter.isValidEndOfType(at: prevIndex), |
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.
Noticed some cases that were previously unsupported by the redundantInit
rule, like [String].init()
, Array<String>.init()
, and String?.init()
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #1640 +/- ##
===========================================
+ Coverage 95.11% 95.28% +0.17%
===========================================
Files 20 20
Lines 22339 22553 +214
===========================================
+ Hits 21247 21490 +243
+ Misses 1092 1063 -29 ☔ View full report in Codecov by Sentry. |
@calda I'm in two minds about this rule because it overlaps a bit with the |
@nicklockwood I had some similar thoughts at first, but I think it feels a bit too weird to put this in behavior in the I'm of course fine with either if you have a preference. A new option like |
@calda I see your point. I'm OK with it being a separate rule but the conflict I see is that if you've specified struct {
let view: UIView = UIView()
} Would be changed to this by the struct Foo {
let view: UIView = .init()
} And then the struct Foo {
let foo = Foo()
} Which is unlikely to be what you wanted. I guess |
We could easily update the For the case where the user has both What do you think? |
940f993
to
b01654a
Compare
updated @nicklockwood: 674460c |
b01654a
to
674460c
Compare
This PR adds a new
preferInferredTypes
rule that converts property declarations with an explicit type (e.g.let foo: Foo = .init()
) to instead use an inferred type (let foo = Foo()
).Another approach I considered, but didn't implement, was to have a more general
propertyDeclarationTypes
rule that can also do the inverse transformation (convertlet foo = Foo()
tolet foo: Foo = .init()
).That conversion is more error-prone, since we don't exactly know what the name of the type is. For example,
Foo()
could be actually be referring to afunc Foo() -> Bar
. A real-world example that comes to mind islet rect = CGRectMake(0, 0, 0, 0)
.