Skip to content

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

Description

@giladronat

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions