realm / realm/SwiftLint

Rule Request: Bound method references

Open
#3,693 1 comment 6 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

New Issue Checklist
New rule request

The idea for the rule would be to disallow usage of the language feature.

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

    We have had a few instances of memory leaks in our app that were caused by bound method references, as described in this implemented Swift Evolution proposal 0269. These are fairly difficult to debug, and it is not obviously clear to the engineer that writes the code that they are doing something that will likely cause a retain cycle.

  2. Provide several examples of what would and wouldn't trigger violations.

class Test {
    var x = 0
    func execute(_ work: @escaping () -> Void) {
        work()
    }
    func method() {
        execute(inc) // This should cause a violation
        execute(self.inc) // This should also cause a violation
        execute({ self.inc() }) // This explicit use of a closure should not trigger a violation
    }
    func inc() {
        x += 1
    }
}
  1. Should the rule be configurable, if so what parameters should be configurable?

    I don't think this needs configuration.

  2. Should the rule be opt-in or enabled by default? Why?

    There is not yet general consensus on this rule.

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 with Swift Evolution proposal 0269 and the bound-method reference examples in this issue. Determine how the requested rule should distinguish direct references such as inc and self.inc from an explicit closure, and resolve whether it should be opt-in or enabled by default. Done means the behavior and configuration decision are sufficiently specified for implementation.

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
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.