Skip to content

Commit

Permalink
Fix unusedArguments shadowing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed May 16, 2022
1 parent bc7f259 commit c918394
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
21 changes: 21 additions & 0 deletions Snapshots/Issues/1183.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
func typeMismatch(
for symbol: String,
index: Int,
expected types: [String],
got: String
) -> RuntimeErrorType {
var types = Set(types).sorted()
if let index = types.firstIndex(of: "block") {
types.append(types.remove(at: index))
}
let expected: String
switch types.count {
case 1:
expected = types[0]
case 2:
expected = "\(types[0]) or \(types[1])"
default:
expected = "\(types.dropLast().joined(separator: ", ")), or \(types.last!)"
}
return .typeMismatch(for: symbol, index: index, expected: expected, got: got)
}
4 changes: 4 additions & 0 deletions Snapshots/Issues/1184.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func foo(index: Int) {
for index in 0 ..< 1 {}
print(index)
}
24 changes: 19 additions & 5 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3675,6 +3675,7 @@ public struct _FormatRules {
var isDeclaration = false
var wasDeclaration = false
var isConditional = false
var isGuard = false
var locals = locals
var tempLocals = Set<String>()
func pushLocals() {
Expand All @@ -3701,6 +3702,8 @@ public struct _FormatRules {
}
let token = formatter.tokens[i]
switch token {
case .keyword("guard"):
isGuard = true
case .keyword("let"), .keyword("var"), .keyword("func"), .keyword("for"):
isDeclaration = true
var i = i
Expand Down Expand Up @@ -3728,15 +3731,26 @@ public struct _FormatRules {
}
}
case .startOfScope("{"):
if !formatter.isStartOfClosure(at: i) {
pushLocals()
}
guard let endIndex = formatter.endOfScope(at: i) else {
argNames.removeAll()
return
}
removeUsed(from: &argNames, with: &associatedData,
locals: locals, in: i + 1 ..< endIndex)
if formatter.isStartOfClosure(at: i) {
removeUsed(from: &argNames, with: &associatedData,
locals: locals, in: i + 1 ..< endIndex)
} else if isGuard {
removeUsed(from: &argNames, with: &associatedData,
locals: locals, in: i + 1 ..< endIndex)
pushLocals()
} else {
let prevLocals = locals
pushLocals()
removeUsed(from: &argNames, with: &associatedData,
locals: locals, in: i + 1 ..< endIndex)
locals = prevLocals
}

isGuard = false
i = endIndex
case .endOfScope("case"), .endOfScope("default"):
pushLocals()
Expand Down
12 changes: 12 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5161,6 +5161,18 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.unusedArguments)
}

func testShadowedUsedArguments4() {
let input = """
func foo(bar: Int) {
if let bar = baz {
return
}
print(bar)
}
"""
testFormatting(for: input, rule: FormatRules.unusedArguments)
}

func testTryArgumentNotMarkedUnused() {
let input = """
func foo(bar: String) throws -> String? {
Expand Down

0 comments on commit c918394

Please sign in to comment.