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
  • Loading branch information
calda committed Apr 25, 2024
1 parent 3ed1b63 commit 3f39a30
Show file tree
Hide file tree
Showing 2 changed files with 51 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 @@ -3432,7 +3432,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
46 changes: 46 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8410,6 +8410,52 @@ 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 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 3f39a30

Please sign in to comment.