Skip to content

Conversation

@miguel-jimenez-0529
Copy link
Contributor

Summary

This PR adds rule RedundantMemberwiseInit to automatically remove redundant memberwise initializers from structs when they only assign parameters to properties with matching names.

With --redundantmemberwise:

- struct Planet {
-   let name: String
-   let mass: Double
-   
-   init(name: String, mass: Double) {
-     self.name = name
-     self.mass = mass
-   }
- }

+ struct Planet {
+   let name: String
+   let mass: Double
+ }

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

🤖 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. [![SwiftFormat: redundantMemberwiseInit](https://img.shields.io/badge/SwiftFormat-redundantMemberwiseInit-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantMemberwiseInit)
Copy link
Member

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

Suggested change
* <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. [![SwiftFormat: redundantMemberwiseInit](https://img.shields.io/badge/SwiftFormat-redundantMemberwiseInit-7B0051.svg)](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. [![SwiftFormat: redundantMemberwiseInit](https://img.shields.io/badge/SwiftFormat-redundantMemberwiseInit-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantMemberwiseInit)

Copy link
Contributor Author

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
Copy link
Member

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

Comment on lines +2748 to +2749
self.mass = max(0, mass)
self.radius = max(0, radius)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice example 👌🏻

@calda
Copy link
Member

calda commented Jun 25, 2025

Remember to update airbnb.swiftformat

@miguel-jimenez-0529 miguel-jimenez-0529 merged commit ba88523 into master Jun 27, 2025
4 checks passed
@miguel-jimenez-0529 miguel-jimenez-0529 deleted the add-redundant-memberwise-init-rule branch June 27, 2025 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants