nicklockwood / SwiftFormat

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

`redundantSelf` causes an unnecessary warning when a closure has specified an actor and uses a name of a property for parameter #1625

Closed juri closed 4 months ago

juri commented 4 months ago

I have this class, which seems to be syntactically correct (at least swift repl is OK with it):

final class Test {
    private var p: Int? = nil

    func f() {
        self.f2(
            closure: { @MainActor [weak self] p in
                print(p)
            }
        )
    }

    func f2(
        closure: @escaping @MainActor (String) -> Void
    ) {
        Task { @MainActor in
            closure("z")
        }
    }
}

And this SwiftFormat configuration:

--rules redundantSelf
--self insert
--swiftversion 5.9

Running swiftformat . --lint gives me these warnings:

Test.swift:6:1: warning: (redundantSelf) Insert/remove explicit self where applicable.
Test.swift:7:1: warning: (redundantSelf) Insert/remove explicit self where applicable.

This seems te caused by @MainActor in the closure. If I rename either the property p or the closure parameter p, the warnings go away too.

This is with SwiftFormat 0.53.1.

nicklockwood commented 4 months ago

@juri fixed in 0.53.2