nicklockwood / SwiftFormat

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

Required `Self` is removed #1503

Closed kitwtnb closed 1 year ago

kitwtnb commented 1 year ago

The following enum is defined.

enum Foo {
    static var value = 0

    static func f(value: Int) {
        Self.value = value
    }
}

In 0.51.15, it is not rewritten.

In 0.52.0, it was rewritten as follows, and the compilation no longer passes.

enum Foo {
    static var value = 0

    static func f(value: Int) {
        value = value
    }
}
nicklockwood commented 1 year ago

@kitwtnb thanks for flagging this, I'll get it fixed. In the meantime, you can work around it by disabling the redundantStaticSelf rule (cc @sjavora).

nicklockwood commented 1 year ago

@kitwtnb fixed in 0.52.1

kitwtnb commented 1 year ago

@nicklockwood
Thank you very much. The above pattern has been fixed, but the following pattern will now remove the required Self.

0.52.1

class Foo {
    static let bar = 0

    func f() {
        let a = Self.bar
    }
}

class Foo {
    static let bar = 0

    func f() {
        let a = bar
    }
}
nicklockwood commented 1 year ago

@kitwtnb I'm not able to reproduce that with 0.52.1. Can you double check you're using the right version and that particular code snippet reproduces the issue?

kitwtnb commented 1 year ago

@nicklockwood

The following steps were used to reproduce the results.

  1. Write code in ./Foo.swift.
class Foo {
    static let bar = 0

    func f() {
        let a = Self.bar
    }
}
  1. Run it using Mint.
    mint run nicklockwood/SwiftFormat@0.52.1 Foo.swift

  2. Self has been removed.

nicklockwood commented 1 year ago

@kitwtnb with that exact process and those exact file contents, I'm seeing no changes. I have found a bug involving let or if let expressions, so for the following code Self is currently removed by 0.52.1, but not for the example you gave, which seems odd.

class Foo {
    static let bar = 0

    func f() {
        if let a = Self.bar {
            print("oops")
        }
    }
}
kitwtnb commented 1 year ago

It is also reproduced in 0.52.1.

However, this has been corrected in 0.52.2. Thank you very much!