NoLeadingUnderscores: allow leading-underscore backing properties#1217
Open
luancamara wants to merge 1 commit into
Open
NoLeadingUnderscores: allow leading-underscore backing properties#1217luancamara wants to merge 1 commit into
luancamara wants to merge 1 commit into
Conversation
The opt-in NoLeadingUnderscores rule flagged a backing property such as `_count` even when a sibling property `count` exists in the same type, which is a common and intentional pattern for naming a backing store. The rule now exempts a leading-underscore property name when a property whose name is the same without the leading underscore is declared in the same member block. Local variables, top-level bindings, and counterparts that are not properties (e.g. methods) are unaffected, and matching is limited to the same type body (extensions are not matched). The carve-out is unconditional (no new configuration option), matching the approach agreed on in the issue discussion. Fixes swiftlang#1150.
b354114 to
556ab1a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #1150.
What
The opt-in
NoLeadingUnderscoresrule flags a backing property like_counteven when a non-underscored sibling propertycountexists in the same type:Using a leading underscore to name a backing store for a same-named property is a common, intentional pattern, so this exempts it from the rule. This matches the carve-out @allevato agreed to in the issue discussion, and is unconditional (no new configuration option).
How
visit(_:IdentifierPatternSyntax)now skips the diagnostic when the name starts with_and a property whose name is the same without the leading underscore is declared directly in the sameMemberBlock. The check walks up from the pattern to its enclosing type member block and scans sibling property declarations — it's stateless and localized.Scoping decisions (each covered by a test):
countdoes not exempt_count.Testing
NoLeadingUnderscoresTests: backing property allowed, non-property counterpart still flags, scoped to the same type, and locals not exempt. All existing cases still pass.swift testpasses.swift run generate-swift-formatto updateDocumentation/RuleDocumentation.mdfor the rule's new documentation comment. Novisitmethods were added or removed, so the generated pipeline/registry/name-cache are unchanged.