realm / realm/SwiftLint

Rule Request: weak, unowned in noescape closures

Open
#2,393 5 comments 31 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Rule Request

noescape_captures

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

No need to write weak, unowned in capture list in noescape closure.

  1. Provide several examples of what would and wouldn't trigger violations.
    Would trigger:
let modifiedArray = array.map { [weak self] value in
    self?.modify(value) ?? ""
}

let modifiedArray2 = array.map { [unowned self] value in
    self.modify(value)
}

Would not trigger:

let modifiedArray = array.map { value in
    modify(value)
}
  1. Should the rule be configurable, if so what parameters should be configurable?
  • Probably we can always require not to write self inside noespace closures. As there is no need for it

  • Probably we should allow/disallow to use capture list without ownership

let modifiedArray = array.map { [modifier = transformer.operations.modifier] value in
    modifier.modify(value)
}
  1. Should the rule be opt-in or enabled by default? Why?

For weak and unowned - Yes

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 locating the noescape_captures rule entry point and review how SwiftLint represents noescape closures and capture lists. Before implementation, clarify whether the rule covers only weak and unowned captures, how plain capture lists are handled, and whether it should be enabled by default; done means the agreed examples are consistently diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.