Skip to content

Commit 0e705f4

Browse files
committed
Don't emit AlwaysUseLowerCamelCase findings for decls that include an override modifier.
Specifically this applies to function and variable decls. When one of those is overriding a decl in a super class, then this specific instance of the decl cannot be renamed to fix the issue. The original decl in the super class must be renamed. The finding is still raised if the super class is linted. Raising the finding in every subclass is noisy and frustrating, since it can't really be fixed in the subclass.
1 parent bd89f0d commit 0e705f4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Sources/SwiftFormatRules/AlwaysUseLowerCamelCase.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
5151
}
5252

5353
public override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
54+
// Don't diagnose any issues when the variable is overriding, because this declaration can't
55+
// rename the variable. If the user analyzes the code where the variable is really declared,
56+
// then the diagnostic can be raised for just that location.
57+
if let modifiers = node.modifiers, modifiers.has(modifier: "override") {
58+
return .visitChildren
59+
}
60+
5461
for binding in node.bindings {
5562
guard let pat = binding.pattern.as(IdentifierPatternSyntax.self) else {
5663
continue
@@ -94,6 +101,13 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
94101
}
95102

96103
public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
104+
// Don't diagnose any issues when the function is overriding, because this declaration can't
105+
// rename the function. If the user analyzes the code where the function is really declared,
106+
// then the diagnostic can be raised for just that location.
107+
if let modifiers = node.modifiers, modifiers.has(modifier: "override") {
108+
return .visitChildren
109+
}
110+
97111
// We allow underscores in test names, because there's an existing convention of using
98112
// underscores to separate phrases in very detailed test names.
99113
let allowUnderscores = testCaseFuncs.contains(node)

Tests/SwiftFormatRulesTests/AlwaysUseLowerCamelCaseTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,25 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
123123
XCTAssertNotDiagnosed(
124124
.nameMustBeLowerCamelCase("test_HappyPath_Through_GoodCode_Throws", description: "function"))
125125
}
126+
127+
func testIgnoresFunctionOverrides() {
128+
let input =
129+
"""
130+
class ParentClass {
131+
var poorly_named_variable: Int = 5
132+
func poorly_named_method() {}
133+
}
134+
135+
class ChildClass: ParentClass {
136+
override var poorly_named_variable: Int = 5
137+
override func poorly_named_method() {}
138+
}
139+
"""
140+
141+
performLint(AlwaysUseLowerCamelCase.self, input: input)
142+
XCTAssertDiagnosed(
143+
.nameMustBeLowerCamelCase("poorly_named_variable", description: "variable"), line: 2, column: 7)
144+
XCTAssertDiagnosed(
145+
.nameMustBeLowerCamelCase("poorly_named_method", description: "function"), line: 3, column: 8)
146+
}
126147
}

0 commit comments

Comments
 (0)