Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2635

Trailing closures should not begin on new lines

    XMLWordPrintable

    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

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              elena.vilchik Elena Vilchik
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: