Skip to content

Commit 7acb409

Browse files
authored
Merge pull request swiftlang#295 from dylansturg/silence_override_warnings
Don't emit AlwaysUseLowerCamelCase for decls that `override`
2 parents bd89f0d + 0e705f4 commit 7acb409

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)