Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in unusedArguments when shadowing function argument with conditional assignment declaration #1687

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
62 changes: 62 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8410,6 +8410,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