Skip to content

Commit

Permalink
Fix bug in unusedArguments when shadowing function argument with cond…
Browse files Browse the repository at this point in the history
…itional assignment declaration (#1687)
  • Loading branch information
calda authored and nicklockwood committed May 12, 2024
1 parent 88f6080 commit 816356e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3413,7 +3413,11 @@ public struct _FormatRules {
}
var i = range.lowerBound
while i < range.upperBound {
if formatter.isStartOfStatement(at: i, treatingCollectionKeysAsStart: false) {
if formatter.isStartOfStatement(at: i, treatingCollectionKeysAsStart: false),
// Immediately following an `=` operator, if or switch keywords
// are expressions rather than statements.
formatter.lastToken(before: i, where: { !$0.isSpaceOrCommentOrLinebreak })?.isOperator("=") != true
{
pushLocals()
wasDeclaration = false
}
Expand Down
62 changes: 62 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8096,6 +8096,68 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.unusedArguments)
}

func testUnusedArgumentWithClosureShadowingParamName() {
let input = """
func test(foo: Foo) {
let foo = {
if foo.bar {
baaz
} else {
bar
}
}()
print(foo)
}
"""
testFormatting(for: input, rule: FormatRules.unusedArguments)
}

func testUnusedArgumentWithConditionalAssignmentShadowingParamName() {
let input = """
func test(foo: Foo) {
let foo =
if foo.bar {
baaz
} else {
bar
}
print(foo)
}
"""
testFormatting(for: input, rule: FormatRules.unusedArguments)
}

func testUnusedArgumentWithSwitchAssignmentShadowingParamName() {
let input = """
func test(foo: Foo) {
let foo =
switch foo.bar {
case true:
baaz
case false:
bar
}
print(foo)
}
"""
testFormatting(for: input, rule: FormatRules.unusedArguments)
}

func testUnusedArgumentWithConditionalAssignmentNotShadowingParamName() {
let input = """
func test(bar: Bar) {
let quux =
if foo {
bar
} else {
baaz
}
print(quux)
}
"""
testFormatting(for: input, rule: FormatRules.unusedArguments)
}

// MARK: redundantClosure

func testRemoveRedundantClosureInSingleLinePropertyDeclaration() {
Expand Down

0 comments on commit 816356e

Please sign in to comment.