-
Notifications
You must be signed in to change notification settings - Fork 240
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
Improve the DontRepeatTypeInStaticProperties rule to better align with its original intent #918
Improve the DontRepeatTypeInStaticProperties rule to better align with its original intent #918
Conversation
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.
I don't think the message is the problem with this rule; the rule implementation itself is far too broad with what it detects. For example, consider one of the examples mentioned in that PR:
remove the suffix 'Data' from the name of the variable 'postgresDataType'
Presuming Data
was the type of that property, that's an absolutely fine property name that doesn't violate this rule.
I don't remember precisely why we used contains
in this implementation, unless it was a lackluster attempt at also catching plural terms. But at a minimum, I think we should be doing a suffix check instead of a contains check.
If you're really interested in exploring this, we could gain more insight by looking at how the compiler itself (in ClangImporter) stems type-name suffixes off the names of imported Objective-C names, and reimplement similar logic here to detect those cases.
Oh, I see. Thank you for your kind advice. |
93da14d
to
cc02552
Compare
I looked into the suffix-related logic in the ClangImporter, but I couldn't find anything particularly useful to add to the
Most of these cases already seem to be handled by the existing logic. Instead, I have modified the |
Due to implementation limitations, the following case cannot be handled: class Foo {
static let defaultFoo = makeFoo()
static func makeFoo() -> Foo {
return Foo()
}
} However, aside from this, the rule now behaves in a way that aligns well with its description in most cases. |
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.
Thanks for the updates!
I did a little more digging and it looks like the code used by the compiler for this lives here (https://github.com/swiftlang/swift/blob/b221a92f695973f15ab26a63c2a9d7599f69435f/lib/Basic/StringExtras.cpp#L1252), and ClangImporter calls into that.
But that also looks like quite a lot of logic to bring over, and I'm not sure how much the logic would pay off compared to the effort it would take to port it over. What you've done here is already a solid improvement!
Oh, I wasn’t familiar with C++ enough to have checked that file. |
Resolve #854
DontRepeatTypeInStaticProperties checks whether the type is included in the static property's name using contains.
Since it triggers a diagnostic not only when the type appears as a suffix but also in other positions, I have appropriately modified the message.