nicklockwood / SwiftFormat

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

`strongifiedSelf` strips some necessary backticks #1670

Closed heiberg closed 1 month ago

heiberg commented 2 months ago

Description

Swift 5.10 (Xcode 15.3) introduced the nonisolated(unsafe) keyword.

In the following line the backticks are required:

nonisolated(unsafe) let `self` = self

The strongifiedSelf rule strips these, which results in the error

🔴 Keyword 'self' cannot be used as an identifier here

Note that the example here is not strongifying a weak self, but rather declaring a non-isolated version of self (useful in certain test cases) .

Environment

nicklockwood commented 2 months ago

@heiberg I've not been able to reproduce this in 0.53.5 or 0.53.6. It's possible that the bug depends on some surrounding context - could you provide the whole method? (or whatever minimal code is needed to reproduce the problem in isolation)

heiberg commented 2 months ago

Sure! Sorry for the delay, I missed the notification here.

Input

class Hello {
    func foo() {
        nonisolated(unsafe) let `self` = self
    }
}

Command

swiftformat --swiftversion 5.10 Input.swift

Expected Output

Unchanged.

Observed Output

class Hello {
    func foo() {
        nonisolated(unsafe) let self = self
    }
}

Unexpected change:

class Hello {
    func foo() {
-        nonisolated(unsafe) let `self` = self
+        nonisolated(unsafe) let self = self
    }
}

Environment

SwiftFormat 0.53.5

heiberg commented 1 month ago

@nicklockwood Was this issue reproducible given the above?

nicklockwood commented 1 month ago

@heiberg sorry, I also missed this update. I believe the issue was that the issue only arises when --swiftversion is set to 4.2 or above, so it wasn't showing up in my test case. I should have a fix shortly.

nicklockwood commented 1 month ago

@heiberg fixed in 0.53.10