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

Rule Request: Unnecessary Closure Params Parantheses #1583

Closed
Jeehut opened this issue May 29, 2017 · 2 comments
Closed

Rule Request: Unnecessary Closure Params Parantheses #1583

Jeehut opened this issue May 29, 2017 · 2 comments
Labels
duplicate Issues that already exist in other terms.

Comments

@Jeehut
Copy link
Collaborator

Jeehut commented May 29, 2017

This is a rule request. I'm willing to implement this myself and send a PR, but before doing that I'd like to know if the rule is wanted or not. To explain the desired rule, I'd like to first post the custom rule we are already using in our projects:

  closure_params_parantheses:
    included: ".*.swift"
    regex: '\{\s*\([^):]+\)\s*in'
    name: "Unnecessary Closure Params Parantheses"
    message: "Don't use parantheses around non-typed parameters in a closure."
    severity: warning

I felt like this rule might be useful for other projects as well, which is why I'm posting it. What do you think about it? For us it helps a lot to prevent common issues and enforce a shared style.

Rationale

When writing closures in Xcode, the Xcode auto-completion tends to always add parantheses around the parameters. I think this is intended by Apple and I also think it makes sense to ensure people don't get confused, when they try to specify the type explicitly (e.g. because of generics) and the code doesn't compile any more due to missing parantheses. But in many cases (if the type is not explicitly specified via : Type) it is unnecessary in Swift to add parentheses. According to the rule to keep code clean and concise, the parentheses should be removed in such cases.

A few examples:

// ❌ not acceptable
URLSession.shared.dataTask(with: request, completionHandler: { (sessionData, _, error) in
    // some code
}

// ✅ acceptable
URLSession.shared.dataTask(with: request, completionHandler: { sessionData, _, error in
    // some code
}

// ❌ not acceptable
viewsForEntries.forEach { (entry, view) in /* some code */ }

// ✅ acceptable
viewsForEntries.forEach { entry, view in /* some code */ }
@marcelofabri
Copy link
Collaborator

Duplicate of #1483

@marcelofabri marcelofabri added the duplicate Issues that already exist in other terms. label May 29, 2017
@Jeehut Jeehut changed the title Rule Request Rule Request: Unnecessary Closure Params Parantheses May 29, 2017
@Jeehut
Copy link
Collaborator Author

Jeehut commented May 29, 2017

Ah, sorry, didn't see that issue. Thanks for marking this as duplicate. I 👍 'd yours! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues that already exist in other terms.
Projects
None yet
Development

No branches or pull requests

2 participants