Closed rogerluan closed 10 months ago
Does the redundantParens rule not already do this?
Nope @nicklockwood 😢 it's not a covered use case/example in the docs, and I also tested and it doesn't modify my code
@rogerluan oh sorry you're right - I was actually thinking of the trailingClosures rule.
I just tested it with all your examples and it seems to work (it's also enabled by default).
Oooh. I've explicitly disable trailing closures for some reason, I can't remember why now. I'll test it and try to remember why I disabled it as it might've been some bug 😅 I'll report back. Thanks for the help!
Ah, I remembered. In one of the projects I worked on, there was a overloaded function that used to cause issues, so I disabled this rule. It was weird, but an overview of the issue would be:
preconditionFailure()
inside of it, intentionally).The issue was that due to some poor API design those two overloaded functions were only differing in the name of the closure argument IIRC (can't remember the details or give concrete examples tbh).
I don't think that is an issue that SwiftFormat should resolve haha and I see no reason to not enable this rule in all other projects :) Thanks for pointing this to me! We can close this issue 🚀
@rogerluan this was actually a known issue with the rule, which was fixed in a later version by limiting it to only anonymous closure arguments (from the known issues section of the README):
The trailingClosures rule can generate ambiguous code if a function has multiple optional closure arguments, or if multiple functions have signatures differing only by the name of the closure argument. For this reason, the rule is limited to anonymous closure arguments by default. You can use the --trailingclosures and --nevertrailing arguments to explicitly opt in or out of trailing closure support for specific functions.
So I think it should be safe to re-enable it for your project now.
That's great to hear!! 🤩 It was indeed a long time ago and I didn't realize this has been fixed since then 😁
Thank you once again!
I did my research :)
New rule request
This new rule would remove redundant parenthesis from closures, as it's a lot cleaner. See examples below:
What would trigger
What wouldn't trigger
I believe there's a consensus that this is the expected way to write Swifty code, but I could be wrong. In fact I was very surprised that SwiftLint doesn't support this already. I'm curious to hear other opinions.