Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Move this trailing closure to line N.
-
Default Severity:Minor
-
Impact:Low
-
Likelihood:Low
-
Default Quality Profiles:Sonar way
-
Covered Languages:Swift
-
Remediation Function:Constant/Issue
-
Constant Cost:2min
Description
When the last arguments to a function are closures, it's possible and often desirable to write these closures after the function's parentheses. These are called trailing closure arguments. In order to help distinguish trailing closure arguments from independent code blocks, it is best to begin the first closure argument on the same line as the function call and each following closure argument on the last line of the preceding one.
Noncompliant Code Example
funWithClosureArgument() { // Noncompliant; looks like an independent code block print("Hello world") } funWith3ClosureArguments { print("Hello world") } b: { // Noncompliant; looks like an independent code block print("Hello world") } c: { // Noncompliant; looks like an independent code block print("Hello world") }
Compliant Solution
funWithClosureArgument() { print("Hello world") } funWith3ClosureArguments { print("Hello world") } b: { print("Hello world") } c: { print("Hello world") }
Attachments
Issue Links
- is implemented by
-
SONARSWIFT-23 Rule: Trailing closure should not begin on separate line
-
- Closed
-
- is related to
-
SONARSWIFT-466 Update rule S2635 for multiple trailing closures
-
- Closed
-