Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantSelf Rule Removal Causes Compilation Error in SwiftFormat 0.50.6 - 0.54.0 #1732

Open
filimo opened this issue Jun 14, 2024 · 2 comments

Comments

@filimo
Copy link

filimo commented Jun 14, 2024

After updating to SwiftFormat versions between 0.50.6 and 0.54.0, the redundantSelf rule incorrectly removes the explicit use of self in some contexts, which leads to compilation errors. The issue appears when using properties within closures or async contexts in actor classes.

Steps to Reproduce

  1. Define an actor with a property.
  2. Access the property within a method, using self explicitly.
  3. Run SwiftFormat version 0.50.6 to 0.54.0 on the codebase.
  4. Observe the removal of self and the subsequent compilation error.

Example

Before running SwiftFormat:

let logger = Logger()

actor TestActor {
    let test = ""

    private func cancel() {
        logger.info("\(String(describing: self.test))")
    }
}

After running SwiftFormat:

let logger = Logger()

actor TestActor {
    let test = ""

    private func cancel() {
        logger.info("\(String(describing: test))")
    }
}

Error:

Reference to property 'test' in closure requires explicit use of 'self' to make capture semantics explicit

Expected Behavior

SwiftFormat should recognize the context in which self is necessary for property access within closures or async methods and avoid removing it.

Actual Behavior

The redundantSelf rule removes the necessary self prefix, causing a compilation error.

Environment

  • SwiftFormat versions: 0.50.6 - 0.54.0
  • Swift version: 6.0
  • Xcode version: 16.0 beta (16A5171c)
@nicklockwood
Copy link
Owner

@filimo I'm guessing the argument to Logger.info() is an autoclosure or something? Unfortunately SwiftFormat can't detect this automatically so you have to manually exclude such cases using --selfrequired info

@filimo
Copy link
Author

filimo commented Jun 15, 2024

The --selfrequired logger.info provides a partial workaround, but the problem persists in contexts involving String(describing:).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants