-
Notifications
You must be signed in to change notification settings - Fork 344
Add RedundantMemberwiseInit rule #318
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
Add RedundantMemberwiseInit rule #318
Conversation
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
README.md
Outdated
|
|
||
| * <a id='time-intensive-init'></a>(<a href='#time-intensive-init'>link</a>) **Avoid performing any meaningful or time-intensive work in `init()`.** Avoid doing things like opening database connections, making network requests, reading large amounts of data from disk, etc. Create something like a `start()` method if these things need to be done before an object is ready for use. | ||
|
|
||
| * <a id='omit-redundant-memberwise-init'></a>(<a href='#omit-redundant-memberwise-init'>link</a>) **Omit redundant memberwise initializers.** The compiler can synthesize memberwise initializers for structs, so explicit initializers that only assign parameters to properties with the same names should be omitted. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantMemberwiseInit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth mentioning that this only applies to internal inits
| * <a id='omit-redundant-memberwise-init'></a>(<a href='#omit-redundant-memberwise-init'>link</a>) **Omit redundant memberwise initializers.** The compiler can synthesize memberwise initializers for structs, so explicit initializers that only assign parameters to properties with the same names should be omitted. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantMemberwiseInit) | |
| * <a id='omit-redundant-memberwise-init'></a>(<a href='#omit-redundant-memberwise-init'>link</a>) **Omit redundant memberwise initializers.** The compiler synthesize internal memberwise initializers for structs, so explicit internal initializers that only assign parameters to properties with the same names should be omitted. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantMemberwiseInit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can apply to private and fileprivate too. Let me update
| let radius: Double | ||
| } | ||
|
|
||
| // ALSO RIGHT: Custom logic in initializer makes it non-redundant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should include an ALSO RIGHT: case with a public struct and a public init
| self.mass = max(0, mass) | ||
| self.radius = max(0, radius) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice example 👌🏻
|
Remember to update |
Summary
This PR adds rule
RedundantMemberwiseInitto automatically remove redundant memberwise initializers from structs when they only assign parameters to properties with matching names.With
--redundantmemberwise:Reasoning
Removing redundant memberwise initializers reduces boilerplate code and improves readability. The compiler can synthesize equivalent memberwise initializers automatically for structs, so explicit initializers that only perform simple property assignment are unnecessary. This rule helps maintain cleaner, more concise code while preserving the same functionality.
🤖 Generated with Claude Code