skip to content

Leading a legacy app's SwiftLint rollout, would you enable opt_in_rules: all or a curated list, which rules are errors, and where does swiftlint analyze run?

level: principalimportance: should knowfreq 35%

answer

  1. every release can add opt-in rules
  2. some opt-in rules pull in opposite directions
  3. severity per rule, strict in CI
  4. analyzer rules need a clean build log
  5. a shared parent_config across modules

basics

~20 s

There is no single right answer: a curated opt_in_rules list with a pinned SwiftLint version keeps upgrades predictable, opt_in_rules: all buys coverage at the cost of noise and conflicting rules, and swiftlint analyze belongs in a scheduled job because it needs a clean build log.

solid answer

~40 s

I would start from the defaults plus a short curated `opt_in_rules` list the team has agreed, pin the SwiftLint version, and add rules one at a time, each with a regenerated baseline in the same change. `opt_in_rules: all` enables every opt-in lint rule, including any a future release adds, and some of them disagree: with defaults, `explicit_type_interface` flags `let url = URL()` while `redundant_type_annotation` flags `let url: URL = URL()`. Severity is a per-rule decision; `force_cast` and `force_try` already default to error, and CI runs `--strict` against the baseline. Analyzer rules such as `unused_import` and `unused_declaration` need `swiftlint analyze` with a compiler log from a clean build, so they run nightly, not on every build. A shared `parent_config` keeps modules consistent.

code

yaml · 15 lines
yaml
# .swiftlint.yml at the repository root
parent_config: Configs/team-base.yml
opt_in_rules:
  - force_unwrapping
  - empty_count
  - sorted_imports
analyzer_rules:
  - unused_import
  - unused_declaration
line_length: 140
file_length:
  warning: 600
  error: 1200
strict: true
baseline: Baseline.json

go deeper

for a junior

Recall that opt-in rules are off unless listed, and that analyzer rules run only through swiftlint analyze.

for a middle

Explain what opt_in_rules: all pulls in, how severities and thresholds are set per rule, and why analyze needs a clean build log.

for a senior

Show the operational habits: pinned version, one rule per change with a regenerated baseline, strict in CI, analyze in a scheduled job.

for a principal

Own the trade-off between coverage and noise, decide how rules are added and who may regenerate the baseline, and write it down for every module.

## The decision a lead owns Once SwiftLint runs on the insurance-claims app, the remaining questions are policy: **which rules**, **how severe**, **how they change over time** and **where the slow ones run**. None has a single right answer; each trades coverage against noise and upgrade cost. (Lint philosophy in general, and rolling out an analyser across many repositories, are covered with static analysis; this is about SwiftLint's own levers.) ## Curated list or opt_in_rules: all | | Curated `opt_in_rules` | `opt_in_rules: all` + `disabled_rules` | |---|---|---| | What runs | exactly the opt-in rules you listed, plus defaults | every opt-in lint rule not disabled | | On a SwiftLint upgrade | new default rules and rule changes arrive | the same, plus every newly added opt-in rule | | Conflicts | you choose consistent rules | possible, you disable one side | | Review effort | each addition is a decision | each upgrade is a review | The README explains why rules are opt-in in the first place: many false positives, too slow, or not general consensus. Turning all of them on adopts every one of those judgements at once. Some opt-in rules also pull in opposite directions. With default options, `explicit_type_interface` reports `var url = URL()` because the property has no type annotation, while `redundant_type_annotation` reports `var url: URL = URL()` because the annotation repeats the initialiser. Both on means one of them fires on every such line until you set `explicit_type_interface`'s `allow_redundancy: true` or disable one. Whichever you choose: - **pin the SwiftLint version** (the plugin package's resolved version, or one binary version for scripts), so an upgrade is a deliberate change with its own pull request; - **add rules one at a time**, regenerating `Baseline.json` in the same change so CI stays green and the diff shows what the rule found; - consider `only_rules` if the team wants even default rules to change only by explicit decision. ## Severity Every rule has a severity, and many have warning and error thresholds: 1. Keep correctness-flavoured rules that the team agrees are always wrong at **error**; `force_cast` and `force_try` already default to error. 2. Leave stylistic rules as **warnings** locally, and run CI with `--strict` (or `strict: true`) against the baseline so new warnings still fail the pull request. 3. Tune thresholds instead of disabling a rule outright: `line_length: 140`, or `file_length` with separate `warning` and `error` values. 4. Decide how suppressions are written: a ` - reason` after every `swiftlint:disable`, with `blanket_disable_command` and `superfluous_disable_command` left on so suppressions stay scoped and current. ## Where swiftlint analyze runs Analyzer rules work on the fully type-checked code: `unused_import`, `unused_declaration`, `explicit_self`, `capture_variable` and `typesafe_array_init`. They are listed under `analyzer_rules` and run only by `swiftlint analyze`, which needs either `--compiler-log-path` with the log of a **clean** `xcodebuild` build (incremental builds do not work) or `--compile-commands` with a compilation database. Without compiler arguments a rule prints that it "Attempted to lint file at path ... without any compiler arguments" and reports nothing. The README warns analyzer rules are considerably slower than lint rules. That shapes the policy: - run `swiftlint analyze` in a **scheduled job** that already performs a clean build, not in every developer build; - `unused_import` is correctable, so a periodic `swiftlint analyze --fix --compiler-log-path xcodebuild.log` run can open a pull request that removes the unused imports, reviewed like any other change; - give the analyzer job its own baseline if it is to gate anything. ## Keeping many modules consistent A large app often has many packages and targets. One team-wide file referenced with `parent_config` gives every module the same base, while a child configuration refines it. A remote `parent_config` over `https://` is fetched again on every run; if the request times out (`remote_timeout`, 2 seconds by default, or `remote_timeout_if_cached`, 1 second, when a cached copy exists) SwiftLint uses the cached copy. A machine that has run SwiftLint once keeps working when the server is slow or down, but a fresh CI runner with no cached copy fails, which makes a base file kept in the repository the safer choice for CI. Nested `.swiftlint.yml` files can relax rules for a subtree such as tests; passing `--config` explicitly turns nested configurations off. The policy that results is a short document plus a configuration file: which rules, at which severity, how one is added, who may regenerate the baseline, and when analyze runs.

  • A SwiftLint upgrade adds 600 violations overnight. With a curated list, how is that possible, and what do you do?
    New releases can add rules that are on by default and change how existing rules behave, and a curated `opt_in_rules` list does not freeze the default set. Treat the upgrade as its own change: read the CHANGELOG, decide per new rule whether to keep, tune or disable it, and regenerate the baseline in that same pull request.
  • Why does the analyze job need a clean build rather than reusing the developer's incremental build log?
    `swiftlint analyze` extracts each file's `swiftc` invocation from the build log. An incremental build only recompiles changed files, so most files have no compiler arguments in the log and analyzer rules report nothing for them. Cleaning first puts every file's invocation in the log.

  • opt_in_rules: all is the safe default for every team.
  • A curated list means upgrades never add violations.
  • Analyzer rules can run on every incremental build.
  • Every SwiftLint rule should be raised to error at once.
  • unused_import works with swiftlint lint and no build log.
  • A remote parent_config is fetched once and never refreshed.