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

Add —nilinit for redundantNilInit rule #1680

Merged
merged 6 commits into from
Apr 20, 2024

Conversation

rakuyoMo
Copy link
Contributor

@rakuyoMo rakuyoMo commented Apr 18, 2024

Related #1677 .

Added —nilinit option to redundantNilInit rule, its value includes insert and remove. Among them, remove is consistent with the previous logic, and insert is a new logic. It will add nil as the default value after the optional value type according to the opposite rules of remove.

I did not implement the properties-only option mentioned in the issue, firstly because I did not need it, and secondly because I was not familiar enough with this project.

I added documentation and test cases and all of them passed.

This is my first PR for this project. If I did something wrong or if there is anything else that needs to be modified, please let me know.

Comment on lines -1922 to +1954
var foo: String?
var foo: String? = nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did I miss adding nil as a default value here? If I understood it wrong, I will change it back, the same below.

Comment on lines +1973 to +1974
var foo: String? = nil
Text(foo ?? "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did I miss adding nil as a default value here? If I understand it wrong, I will change it back, same as above.

@rakuyoMo
Copy link
Contributor Author

I fixed some issues and can now start the review.

@nicklockwood
Copy link
Owner

Thank you - this looks excellent. One thing I didn't previously consider is the case where the value is already being initialized in the init method, e.g.

class Foo {
    var foo: Int?// probably don't want to add = nil here?

    init() {
        foo = nil
    }
}

I checked and I don't think setting the value to nil twice will actually cause any compilation problems or changes in behavior, so it's probably fine to consider handling this an enhancement rather than a blocker for the feature though.

Copy link

codecov bot commented Apr 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.18%. Comparing base (4e3bd75) to head (bd94a43).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1680      +/-   ##
===========================================
+ Coverage    92.81%   95.18%   +2.36%     
===========================================
  Files           20       20              
  Lines        21162    22964    +1802     
===========================================
+ Hits         19642    21858    +2216     
+ Misses        1520     1106     -414     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rakuyoMo
Copy link
Contributor Author

rakuyoMo commented Apr 19, 2024

One thing I didn't previously consider is the case where the value is already being initialized in the init method.

If there is a rule in the future (I'm not sure if there is one now): "All properties that do not rely on external input should have their initial values set when they are defined, rather than set in the init method" (Let’s call it define-init), then this problem will become simpler.

In my opinion, the correct solution to the situation you proposed is as follows:

// --nilinit insert

class Foo {
-  var foo: Int?
+  var foo: Int? = nil

  // If init is empty, the entire init should also be removed. 
  // But I remember that there seems to be another rule that can automatically remove empty methods. 
  // I'm not sure how SwiftFormat handles the same parts between the two rules, is it implemented twice?
  //
  // Considering that the `remove` option does not handle the situation of `var foo: Optional<Int> = nil` 
  // (there are other rules to process `Optional<Int>` as `Int?`), 
  // so I guess there should be no need to delete the empty init method here?
  init() {
-    foo = nil
  }
}

I'm not currently dealing with the init internals and I'd like to hear your thoughts. If define-init exists, then --nilinit insert does not need to consider this situation? Or should we consider this situation anyway.


After I implement the above logic, properties-only or variables-only are still not needed for me. I'm sorry that I may not have the time or energy to implement it, unless you tell me that these two options will be necessary conditions for insert, or you have any other suggestions for how I can implement the above.

@rakuyoMo
Copy link
Contributor Author

Please let me list other situations:

// --nilinit insert

class Foo {
-  var foo: Int?
+  var foo: Int? = nil

  init() {
-    foo = nil

-    var foo: String?
+    var foo: String? = nil
  }
}
// --nilinit insert-only-properties

class Foo {
-  var foo: Int?
+  var foo: Int? = nil

  init() {
-    foo = nil

    var foo: String? // nil will not be automatically inserted
  }
}
// --nilinit insert-only-variables

class Foo {
  var foo: Int? // nil will not be automatically inserted

  init() {
    foo = nil

-    var foo: String?
+    var foo: String? = nil
  }
}

@nicklockwood
Copy link
Owner

@rakuyoMo thanks for the update - I was not actually expecting you to fix this!

After I implement the above logic, properties-only or variables-only are still not needed for me. I'm sorry that I may not have the time or energy to implement it, unless you tell me that these two options will be necessary conditions for insert, or you have any other suggestions for how I can implement the above.

This is absolutely fine not to include.

@nicklockwood nicklockwood merged commit 8ed5abf into nicklockwood:develop Apr 20, 2024
7 checks passed
nicklockwood pushed a commit that referenced this pull request Apr 20, 2024
nicklockwood pushed a commit that referenced this pull request Apr 20, 2024
@rakuyoMo rakuyoMo deleted the feature/redundantNilInit branch April 20, 2024 23:21
nicklockwood pushed a commit that referenced this pull request Apr 22, 2024
nicklockwood pushed a commit that referenced this pull request Apr 22, 2024
nicklockwood pushed a commit that referenced this pull request Apr 22, 2024
nicklockwood pushed a commit that referenced this pull request May 12, 2024
nicklockwood pushed a commit that referenced this pull request May 12, 2024
nicklockwood pushed a commit that referenced this pull request May 17, 2024
nicklockwood pushed a commit that referenced this pull request May 18, 2024
nicklockwood pushed a commit that referenced this pull request May 18, 2024
nicklockwood pushed a commit that referenced this pull request May 18, 2024
nicklockwood pushed a commit that referenced this pull request May 18, 2024
nicklockwood pushed a commit that referenced this pull request May 18, 2024
nicklockwood pushed a commit that referenced this pull request May 18, 2024
nicklockwood pushed a commit that referenced this pull request May 24, 2024
nicklockwood pushed a commit that referenced this pull request May 28, 2024
nicklockwood pushed a commit that referenced this pull request Jun 1, 2024
nicklockwood pushed a commit that referenced this pull request Jun 5, 2024
nicklockwood pushed a commit that referenced this pull request Jun 5, 2024
nicklockwood pushed a commit that referenced this pull request Jun 8, 2024
nicklockwood pushed a commit that referenced this pull request Jun 9, 2024
nicklockwood pushed a commit that referenced this pull request Jun 9, 2024
nicklockwood pushed a commit that referenced this pull request Jun 9, 2024
nicklockwood pushed a commit that referenced this pull request Jun 9, 2024
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.

None yet

2 participants