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

Issue with the rule redundantInit not playing nicely with preprocessor #1624

Open
Nirma opened this issue Feb 9, 2024 · 3 comments
Open

Comments

@Nirma
Copy link

Nirma commented Feb 9, 2024

I love swiftformat and use it often, but unfortunately I came across an issue with the rule redundantInit.

I had a struct that was defined something like this:

struct Item {
    let name: String

    init(name: String) {
        self.name = name
    }

    #if SOMETHING
    init(customType: CustomType) {
        ...
    }
    #endif
}

The issue is that when SOMETHING evaluates to true the special initializer init(customType: CustomType) is defined but that causes the swift compiler to no longer provide a synthesized initializer if redundantInit deletes the normal initializer which was provided explicitly so that it is accessible even when compiling with the flag SOMETHING set to true.

I know another way that would work with the current implementation of redundantInit would be to provide the initializer init(customType: CustomType) via an extension that is defined conditionally by the flag SOMETHING.

struct Item {
    let name: String
}

#if SOMETHING
extension Item {
    init(customType: CustomType) {
        ...
    }
}
#endif

...come to think of it this is cleaner perhaps but I still wanted to confirm if this is expected behavior or not.

Thanks!

@nicklockwood
Copy link
Owner

@Nirma this is a bug. I'll try to get it fixed, but your proposed workaround seems reasonable in the meantime.

@nicklockwood
Copy link
Owner

@Nirma I've not been able to reproduce the actual bug here. I think maybe your code sample was too simplified to reproduce it? What did SwiftFormat actually do to mangle your original code?

@Nirma
Copy link
Author

Nirma commented Feb 24, 2024

@nicklockwood Yes, I am guilty as charged with oversimplifying the example code too much!

Will try to recreate the issue with the offending code later and remove any specifics to the project.

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