Shorthand Argument

Shorthand arguments like $0, $1, etc. in closures can be confusing. Avoid using them too far away from the beginning of the closure. Optionally, while usage of a single shorthand argument is okay, more than one or complex ones with field accesses might increase the risk of obfuscation.

  • Identifier: shorthand_argument
  • Enabled by default: No
  • Supports autocorrection: No
  • Kind: style
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning
    allow_until_line_after_opening_brace 4
    always_disallow_more_than_one false
    always_disallow_member_access false

Non Triggering Examples

f { $0 }
f {
    $0
  + $1
  + $2
}
f { $0.a + $0.b }
//
// allow_until_line_after_opening_brace: 1
//

f {
    $0
  +  g { $0 }

Triggering Examples

f {
    $0
  + $1
  + $2

  + $0
}
//
// allow_until_line_after_opening_brace: 5
//

f {
    $0
  + $1
  + $2
  +  5
  + $0
  + $1
}

//
// always_disallow_more_than_one: true
//

f { $0 + $1 }

//
// allow_until_line_after_opening_brace: 3
// always_disallow_member_access: true
//

f {
    $0.a
  + $0.b
  + $1
  + $2.c
}