nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.92k stars 639 forks source link

unusedArguments incorrectly removes argument when same variable name is used in an if let #1481

Closed fivegreenapples closed 1 year ago

fivegreenapples commented 1 year ago

I found this fairly minimal test case causes the number argument to be stripped incorrectly by unusedArguments:

   func doSomething(with number: Int) {
        if let number = Int?(123),
           number == 456
        {
            print("Not likely")
        }

        if number == 180 {
            print("Bullseye!")
        }
    }

Interestingly, if you remove the number == 456 it parses fine. i.e. this is left untouched and compiles:

    func doSomething(with number: Int) {
        if let number = Int?(123) {
            print("Not likely")
        }

        if number == 180 {
            print("Bullseye!")
        }
    }
nicklockwood commented 1 year ago

@fivegreenapples fixed in 0.51.13

fivegreenapples commented 1 year ago

Awesome! Thank you!

FelixLisczyk commented 1 year ago

I have the same problem in 0.51.13. Should I create a new issue?

Example:

private func isValid(indexPath: IndexPath?) {
    if let indexPath, indexPath.row < self.tableView.numberOfRows(inSection: 0) {
        return true
    } else {
        return false
    }
}