realm / realm/SwiftLint

Rule Request: Closure Parameter Count

Open
#6,901 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discussion rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
New Rule Request

function_parameter_count limits arity for function and method declarations, but there is no equivalent for closures.

The original request that produced that rule, #415, asked for a warning on methods/functions/closures with too many parameters. The implementation only visits FunctionDeclSyntax (FunctionParameterCountRule), so closure expressions, trailing closures, and closure type signatures are never counted.

High-arity closures have the same readability problem as high-arity functions (call sites and bodies are hard to follow; “introduce parameter object” still applies), and they are easy to miss because $0…$n does not look like a long parameter list.

Related existing rules cover position, unused names, and body length (closure_parameter_position, unused_closure_parameter, closure_body_length), not count.

1. Why should this rule be added?

Same rationale as function_parameter_count: a large parameter list is a complexity smell. Closures are a gap in that metric.

Community / prior discussion:

  • #415 (“Limit number of method/closure parameters rule”) — implemented for functions only; inits later excluded
  • #4217 (constructor parameter count) — similar arity metric, still open; this request is the closure side of the same idea
2. Examples

Assume a warning threshold of 5 (matching function_parameter_count defaults).

Would not trigger:

let sum = numbers.reduce(0) { $0 + $1 }

items.enumerated().map { index, item in
    (index, item)
}

let handler: (Int, Int, Int) -> Void = { a, b, c in
    print(a, b, c)
}

// Capture list is not a parameter list
{ [weak self, unowned delegate] value in
    self?.handle(value, delegate)
}

Would trigger:

↓{ a, b, c, d, e, f in
    a + b + c + d + e + f
}

items.combine { ↓a, b, c, d, e, f in
    (a, b, c, d, e, f)
}

// Shorthand: highest index implies 6 parameters
_ = values.map { ↓$0 + $1 + $2 + $3 + $4 + $5 }

let handler: ↓(Int, Int, Int, Int, Int, Int) -> Void
3. Configuration

Mirror function_parameter_count:

  • warning / error severity thresholds (suggested defaults: warning 5, error 8)
  • Whether unused _ parameters count (suggest yes: they still contribute arity)
  • Whether shorthand $n counts as n + 1 parameters (suggest yes)
  • Whether closure type signatures (e.g. (A, B, C, D, E, F) -> R) are included, or only closure expressions (could be a boolean, defaulting to expressions only if type-signature noise is a concern)
  • Capture-list items should not count
4. Opt-in or enabled by default?

Opt-in. Apple and third-party APIs often use high-arity callbacks; enabling this by default would be noisy in a way function_parameter_count is not. Projects that already care about function arity can turn it on independently (and with its own thresholds).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing FunctionParameterCountRule implementation and the related closure rules, including closure_parameter_position, unused_closure_parameter, and closure_body_length. Then inspect their syntax visitors, configuration, and tests to determine how closure expressions and shorthand parameters are represented. Done means an opt-in closure arity rule with documented threshold behavior and coverage for the examples and configuration choices in this issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.