SwiftLint
SwiftLint copied to clipboard
Multiline arguments violation with multiple trailing closures
New Issue Checklist
- [x] Updated SwiftLint to the latest version
- [x] I searched for existing GitHub issues
Describe the bug
For below code
talkProvider.getAvailability(forUser: userId) { [weak self] response in
self?.handleSuccess(response)
} onError: { [weak self] _ in
self?.handleError()
}
I receive this warning:
Multiline Arguments Violation: Arguments should be either on the same line, or one per line. (multiline_arguments)
Environment
- SwiftLint version (run
swiftlint version
to be sure)? 0.43.1 - Installation method used (Homebrew, CocoaPods, building from source, etc)? Pods
- Are you using nested configurations? nope
- Which Xcode version are you using (check
xcodebuild -version
)? Xcode 12.4 Build version 12D4e - Do you have a sample that shows the issue? Yes, it is.
Done linting! Found 1 violation, 0 serious in 1 file.
To resolve this warning I did following changes to function declaration
Existing function declaration:
func getAvailability(forUser: Int, _ onSuccess: @escaping (String) -> Void, onError: (String) -> Void) { }
Updated function declaration
func getAvailability(forUser: Int, onSuccess: @escaping (String) -> Void, onError: (String) -> Void) { }
Usage of updated function:
getAvailability(forUser: 10, onSuccess: { (_) in
}, onError: { (_) in
})
@Sonal-Kachare Thank you for your answer! Of course, the code you've proposed is correct, but unfortunately, it doesn't use the trailing closure feature, which is powerful and recommended in Swift 5.3+.
When you use a SwiftUI TextField, Xcode will generate this code for you:
TextField("Title", text: $text) { isEditing in
// ...
} onCommit: {
// ...
}
So this is definitely the recommended syntax and should not trigger a multiline arguments violation.
@marcelofabri could you take a look at this, please :)?
I run into a similar issue with multiple trailing closures but for the vertical_parameter_alignment_on_call
rule:
PreviewView(
previewLayout: .fitWidth(360)
) {
SomeView()
} update: {
$0.update(data: data)
}
Which creates the following warning:
Vertical Parameter Alignment On Call Violation: Function parameters should be aligned vertically if they're in multiple lines in a method call. (vertical_parameter_alignment_on_call)
Any news on the status of this issue? I'm still getting this warning in version 0.47.1.
Also seeing this in 0.47.1.
Custom signature:
init(
_ title: String,
@ViewBuilder leftButtonBuilder: () -> LeftButton,
@ViewBuilder rightButtonBuilder: () -> RightButton
)
Failing Invocation (multiline_arguments
):
TitleBar("Profile") {
backButton
} rightButtonBuilder: {
editButton
}
Also tried...
Fails multiline_arguments_brackets
and vertical_parameter_alignment_on_call
:
TitleBar("Profile")
{
backButton
} rightButtonBuilder: {
editButton
}
Works, but not ideal:
TitleBar(
"Profile",
leftButtonBuilder: {
backButton
}, rightButtonBuilder: {
editButton
}
)
It seems like a workaround would be config it differently:
multiline_arguments:
only_enforce_after_first_closure_on_first_line: true
Please refer to https://realm.github.io/SwiftLint/multiline_arguments.html
This should've been fixed by https://github.com/realm/SwiftLint/pull/4750