Contents

p-x9/swift-weak-self-check

A CLI tool for `[weak self]` detection by `swift-syntax`

Usage

CLI

OVERVIEW: Check whether `self` is captured by weak reference in Closure.

USAGE: weak-self-check [<path>] [--report-type <report-type>] [--quick] [--silent] [--config <config>] [--index-store-path <index-store-path>]

ARGUMENTS:
  <path>                  Path

OPTIONS:
  --report-type <report-type>
                          Detected as `error` or `warning` (default: error)
  --quick                 Check more quicklys. (Not accurate as indexPath is
                          not used)
  --silent                Do not output logs
  --config <config>       Config (default: .swift-weak-self-check.yml)
  --index-store-path <index-store-path>
                          Path for IndexStore
  -h, --help              Show help information.

SPM Plugin

  • WeakSelfCheckBuildToolPlugin

BuildToolPlugin

  • WeakSelfCheckCommandPlugin

CommandPlugin

Configuration

It is possible to customise the configuration by placing a file named .swift-weak-self-check.yml.

Example file is available here: swift-weak-self-check.yml

Heuristic

Detection is performed under the following conditions and a warning/error is reported.

  1. All functions called within a class are subject to traversal.

- If it is in an extension, a decision is made as to whether it is a class or not based on the index-store information. - In quick mode, all functions in the extension are checked, regardless of whether they are classes or not.

  1. Any closure present as an argument of the function is checked.

- If the closure type is specified with a type defined by typealias, it is missed.

  1. If the function is included in a whitelist in the config, it is skipped.
  1. Check that self is used in the closure without [weak self] or [unowned self].
  1. Check that @escaping attribute is attached to the closure type of the function being called.

- Information from the index-store is used. (So, not checked in quick mode). - It is not checked for c and objc functions. - If multiple closures are present in the function argument without labels, this check is not performed.

  1. If the closure type is of type Optional, the warning is applicable even if the @escaping attribute is not attached.

- In the case of optional closure types, there are cases where circular references are produced even when @escaping is not attached.

  1. warning/error is reported

License

swift-weak-self-check is released under the MIT License. See LICENSE

Package Metadata

Repository: p-x9/swift-weak-self-check

Default branch: main

README: README.md