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

"Multiple Closures With Trailing Closure" support for multiple trailing closures (Swift 5.3) #3295

Closed
2 tasks done
giladronat opened this issue Aug 10, 2020 · 1 comment · Fixed by #3353
Closed
2 tasks done

Comments

@giladronat
Copy link

New Issue Checklist

Describe the bug

SwiftLint's 'Multiple Closures With Trailing Closure' rule flags the use of Swift 5.3's new multiple trailing closure syntax (SE-209) on the last closure.

Example:

Button {
    print("Button tapped")
} label: { // Multiple Closures with Trailing Closure Violation
    Text("Button")
}

Proposed Fix/Behavior

The new syntax serves to alleviate the call site confusion that initially motivated creating the rule. Thus, the rule implementation could be adjust to detect multiple closures with trailing closure(s) -- flag when a non-trailing closure is used as an argument that has either a single trailing closure or multiple trailing closures.

This would mean this would get flagged:

Button(action: { print("Button tapped") }) { // Multiple Closures with Trailing Closure Violation
    Text("Button")
}

But this would not:

Button {
    print("Button tapped")
} label: {
    Text("Button")
}

And this would not:

UIView.animate(withDuration: 1.0) {
    self.view.alpha = 0
} completion: { _ in
    self.view.removeFromSuperview()
}
Complete output when running SwiftLint, including the stack trace and command used
$ echo "Button {
    print(\"Button tapped\")
} label: {
    Text(\"Button\")
}" | swiftlint lint --no-cache --use-stdin --enable-all-rules

Loading configuration from '.swiftlint.yml'
<nopath>:3:3: warning: Multiple Closures with Trailing Closure Violation: Trailing closure syntax should not be used when passing more than one closure argument. (multiple_closures_with_trailing_closure)
Done linting! Found 1 violation, 0 serious in 1 file.

Environment

  • SwiftLint version: 0.40.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc): Homebrew
  • Paste your configuration file:
    Empty file. Relevant rule: multiple_closures_with_trailing_closure.
  • Are you using nested configurations?
    No.
  • Which Xcode version are you using (check xcodebuild -version)?
    Xcode 12.0 Beta 4 (12A8179i)
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
Button {} label: { Text("Label") }
echo "Button {} label: { Text(\"Label\") }" | swiftlint lint --no-cache --use-stdin --enable-all-rules
@Jumhyn
Copy link
Contributor

Jumhyn commented Sep 18, 2020

I've put up a tentative fix for this issue here.

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 a pull request may close this issue.

2 participants