Swiftlint

作者 dpearson26998d90fd121a26無授權條款1.1K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫2 個月前更新

Configures and enforces SwiftLint in Swift projects using build tool plugins, run scripts, and CI. Covers .swiftlint.yml configuration, disabled_rules, opt_in_rules, only_rules, analyzer_rules, baselines, autocorrect, swiftlint:disable suppressions, reporter formats (sarif, json, checkstyle), strict and lenient modes, SwiftLintBuildToolPlugin via SimplyDanny/SwiftLintPlugins, swift package plugin swiftlint, Xcode run script phases, CI integration, multiple configuration files, and rollout strategies for existing codebases. Use when setting up SwiftLint, configuring lint rules, suppressing warnings, creating baselines, choosing between build tool plugin and run script, or integrating SwiftLint into CI.

AI 產生的概覽

指導在 Swift 專案中設定與執行 SwiftLint,涵蓋規則、抑制、基準線與 CI 整合。

功能
此技能提供在 Swift 程式碼庫中設定與執行 SwiftLint 的說明。內容涵蓋 .swiftlint.yml 中的 disabled_rules、opt_in_rules、only_rules 與 analyzer_rules 等選項,以及抑制註解、基準線、自動修正、報告格式與嚴格或寬鬆模式。它也說明整合方式的選擇,包括 SwiftLintBuildToolPlugin、SwiftPM 命令外掛、Xcode 執行腳本建置階段、CI 流程與分層設定檔。
適用情境
適用於在新的或既有的 Swift 專案中導入 SwiftLint、在建置工具外掛與執行腳本之間做選擇,或把程式碼檢查接入 CI。也適用於調整規則、抑制警告、建立基準線,或在既有程式碼庫中逐步推行檢查。
執行需求
此技能不附帶腳本,僅包含說明與參考文件。實際使用需要 SwiftLint 本身、與專案相容的 Swift 工具鏈,可選地需要 SimplyDanny/SwiftLintPlugins 套件、Homebrew、Docker 或 CI 執行環境;部分參考流程需要網路存取以取得套件或遠端設定。

SwiftLint

SwiftLint enforces Swift style and conventions by linting source files against a configurable rule set. This skill covers setup, configuration, rule selection, suppression, CI integration, and rollout strategy.

SwiftLint is a style enforcement tool, not a style guide. For underlying Swift naming and design conventions, see swift-api-design-guidelines. For architecture patterns, see swift-architecture.

Contents


Recommended Setup

Default: build tool plugin via SimplyDanny/SwiftLintPlugins.

Add the plugin package to Package.swift or via Xcode's package dependencies:

swift
// Package.swiftdependencies: [  .package(url: "https://github.com/SimplyDanny/SwiftLintPlugins", from: "<reviewed-version>")]

For SwiftPM targets, apply the plugin:

swift
.target(    name: "MyApp",    plugins: [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins")])

For Xcode projects without a Package.swift, add the package dependency in the project settings, then enable the plugin under the target's Build Phases or the package's plugin trust dialog.

The build tool plugin runs SwiftLint automatically on every build. No run script required.

First build: Xcode prompts to trust the plugin. Select "Trust & Enable All" for the SwiftLintPlugins package.

For alternatives (run scripts, command plugin, Homebrew CLI), see references/plugins-run-scripts-and-integrations.md [blocked].

Configuration

Create .swiftlint.yml at the project root. SwiftLint loads the main configuration from the invocation or plugin working directory, then can merge the nearest nested .swiftlint.yml for each file when configs are discovered automatically. Passing --config overrides automatic discovery and disables nested-config lookup.

yaml
# .swiftlint.yml — conservative starter configdisabled_rules:  - trailing_whitespace  - todo
opt_in_rules:  - empty_count  - closure_spacing  - force_unwrapping  - sorted_imports  - vertical_whitespace_opening_braces  - private_swiftui_state  - unhandled_throwing_task  - accessibility_label_for_image
included:  - Sources  - Tests
excluded:  - .build  - DerivedData  - "**/.build"  - "**/Generated"
line_length:  warning: 140  error: 200
type_body_length:  warning: 300  error: 500
file_length:  warning: 500  error: 1000

Key configuration options:

KeyPurpose
disabled_rulesTurn off default-enabled rules
opt_in_rulesTurn on rules not enabled by default
only_rulesUse only the listed rules (mutually exclusive with disabled_rules/opt_in_rules)
analyzer_rulesRules requiring compiler logs (run via swiftlint analyze)
baselinePath to an existing baseline file used to suppress known violations
write_baselinePath where SwiftLint should write a new baseline file
includedPaths to lint (default: current directory)
excludedPaths to skip
strictElevate all warnings to errors
lenientDowngrade all errors to warnings
allow_zero_lintable_filesSuppress the error when no Swift files are found
reporterOutput format: xcode (default), json, checkstyle, sarif, csv, emoji, etc.

For full configuration details including severity tuning, environment-variable interpolation, and nested/remote configs, see references/adoption-and-configuration.md [blocked].

Rule Selection Strategy

SwiftLint ships with three rule categories:

  1. Default rules — enabled automatically, cover widely accepted conventions
  2. Opt-in rules — disabled by default, enable selectively via opt_in_rules
  3. Analyzer rules — require compiler logs, enabled via analyzer_rules

Browse the full categorized list at https://realm.github.io/SwiftLint/rule-directory.html.

Recommended approach for new projects:

  1. Start with defaults. Run swiftlint rules to see which rules are enabled.
  2. Disable rules that conflict with your team's established conventions.
  3. Add opt-in rules one at a time. Review violations before committing each addition.
  4. Do not use only_rules unless you have a specific reason to start from zero.

Recommended approach for existing codebases:

  1. Start with the default rule set.
  2. Create a baseline (see Baselines) to suppress all existing violations.
  3. Run the same strict, baseline-aware command used by CI and fix every new violation.
  4. Re-run until green before enabling another rule or starting the next cleanup batch.
  5. Burn down baseline violations incrementally without accepting baseline growth.

Do not transcribe or memorize the rule directory. Look up rule identifiers and configuration options at the official rule directory when needed.

Suppressions

Suppress SwiftLint for specific lines when a rule produces a false positive or when the violation is intentional and reviewed.

swift
// swiftlint:disable:next force_castlet view = object as! UIView
let legacy = try! JSONDecoder().decode(T.self, from: data) // swiftlint:disable:this force_try
// swiftlint:disable:previous large_tuple

Disable for a region:

swift
// swiftlint:disable cyclomatic_complexityfunc complexRouter(...) { ... }// swiftlint:enable cyclomatic_complexity

Disable all rules (use sparingly):

swift
// swiftlint:disable all// ... generated or legacy code ...// swiftlint:enable all

Policy:

  • Prefer targeted single-rule suppressions over all.
  • Always re-enable after the region ends.
  • For generated code, prefer excluded paths in .swiftlint.yml over inline suppressions.
  • For test targets with different tolerance, use a child configuration (see Multiple Configurations).

For full suppression syntax, see references/rules-suppressions-and-baselines.md [blocked].

Baselines

Baselines let you adopt SwiftLint in an existing codebase without fixing every legacy violation first.

Create a baseline:

sh
swiftlint --write-baseline .swiftlint.baseline

This records all current violations. Future runs compare against this baseline and only report new violations.

Use the baseline:

sh
swiftlint --baseline .swiftlint.baseline

In CI, pass --baseline so only new violations fail the build. Burn down the baseline over time by fixing legacy violations and regenerating.

For baseline workflows and rollout strategy, see references/rules-suppressions-and-baselines.md [blocked].

Autocorrect

SwiftLint can fix some violations automatically:

sh
swiftlint --fix# or the legacy alias:swiftlint --autocorrect

Warnings:

  • Never run --fix as a pre-compile build phase. Auto-fixes modify source files. If run automatically on every build, this creates an unpredictable edit-build loop and can mask real issues.
  • Run --fix manually or in a dedicated CI step, then review the diff.
  • Not all rules support autocorrect. Check swiftlint rules — the "Correctable" column shows which rules can auto-fix.
  • Always commit or stash before running --fix.

CI Integration

CI is the primary enforcement surface. A CI check ensures no one merges code that increases the violation count.

Recommended CI pattern:

yaml
# GitHub Actions example- name: Lint  run: |    brew install swiftlint    swiftlint --strict --reporter sarif > swiftlint.sarif

Key CI options:

FlagEffect
--strictExits non-zero on warnings (not just errors)
--reporter sarifGitHub Advanced Security compatible output
--reporter jsonMachine-readable output
--reporter checkstyleJenkins/SonarQube compatible
--baseline .swiftlint.baselineOnly fail on new violations

For SARIF upload to GitHub code scanning, add github/codeql-action/upload-sarif after the lint step.

After any configuration, baseline, or rule change, run the exact CI lint command locally or in a validation job. On a nonzero exit, inspect and fix the new violations, then rerun the same command until it passes; do not regenerate the baseline merely to hide the failure.

For full CI recipes and reporter details, see references/plugins-run-scripts-and-integrations.md [blocked].

Integration Decision Tree

Choose how to run SwiftLint based on project shape:

ScenarioRecommended integration
SwiftPM package or Xcode project with Package.swiftBuild tool plugin via SwiftLintPlugins
SwiftPM project needing CLI flags (--fix, --baseline)Command plugin: swift package plugin swiftlint
Xcode project without SwiftPM, team uses HomebrewRun script build phase
CI/CD pipelineHomebrew or Docker install, run swiftlint directly
Pre-commit hookHomebrew install + .pre-commit-config.yaml or git hook script

The build tool plugin is preferred for local development because it requires no PATH configuration, pins the SwiftLint version via package resolution, and runs automatically on build.

For detailed setup instructions for each integration, see references/plugins-run-scripts-and-integrations.md [blocked].

Multiple Configurations

SwiftLint supports layered configuration files. A .swiftlint.yml in a subdirectory inherits from and overrides the parent config.

Common patterns:

  • Relaxed test config: place a .swiftlint.yml in Tests/ that disables force_unwrapping and raises file_length
  • Strict module config: place a stricter .swiftlint.yml in a shared module directory
  • Remote config: use parent_config with an HTTPS URL to pull a shared team config (caching supported)
yaml
# Tests/.swiftlint.yml — child configdisabled_rules:  - force_unwrapping  - force_try
file_length:  warning: 800

You can also pass multiple configs on the CLI:

sh
swiftlint --config .swiftlint.yml --config .swiftlint-extra.yml

Later configs override earlier ones for overlapping keys.

For nested config resolution, remote configs, and CLI multi-config details, see references/adoption-and-configuration.md [blocked].

Common Mistakes

  1. Running --fix in a build phase. Auto-fixing on every build creates unpredictable source modifications. Run --fix manually.

  2. Using only_rules without understanding the implication. This disables all rules except those listed. Most teams should use disabled_rules + opt_in_rules instead.

  3. Suppressing with // swiftlint:disable all and forgetting to re-enable. This silently disables all linting for the rest of the file.

  4. Not pinning the SwiftLint version. Different versions have different default rules. Use the build tool plugin (version pinned via SPM) or pin in your Brewfile / CI config.

  5. Excluding too broadly. Excluding Tests/ entirely means test code gets no linting. Use a child config with relaxed rules instead.

  6. Ignoring the toolchain mismatch. SwiftLint must be built with (or compatible with) the same Swift toolchain used to compile your project. Mismatches cause parsing errors. See references/plugins-run-scripts-and-integrations.md [blocked] for multi-toolchain guidance.

  7. Adopting too many opt-in rules at once in a large codebase. This creates an overwhelming number of violations. Add rules incrementally and use baselines.

  8. Not configuring included paths. Without included, SwiftLint scans the working directory recursively, which may pick up vendored or generated code.

Review Checklist

  • .swiftlint.yml exists at the project root with explicit included/excluded paths
  • SwiftLint version is pinned (via SPM plugin resolution, Brewfile, or CI config)
  • Build tool plugin is enabled for each target that should be linted
  • CI runs swiftlint --strict (or with --baseline for incremental adoption)
  • Baseline does not grow unintentionally, and CI is green before the next rule or cleanup batch
  • No --fix / --autocorrect in build phases
  • Inline suppressions target specific rules, not all
  • Inline suppressions include a comment explaining why
  • Test targets have appropriate config (relaxed rules via child config, not excluded entirely)
  • Autocorrect changes are reviewed in a separate commit
  • New opt-in rules are added one at a time with team consensus

References

  • references/adoption-and-configuration.md [blocked] — Installation paths, .swiftlint.yml deep dive, severity tuning, environment variables, nested/remote configs, rollout strategy
  • references/plugins-run-scripts-and-integrations.md [blocked] — Build tool plugin, command plugin, run scripts, CI recipes, multi-toolchain guidance, VS Code, Fastlane, Docker, pre-commit
  • references/rules-suppressions-and-baselines.md [blocked] — Default vs opt-in vs analyzer rules, suppression syntax, baseline workflows, false-positive handling
  • references/rule-reference.md [blocked] — Bundled exhaustive rule index for local lookup; verify current details with swiftlint rules or the official rule directory
  • references/custom-rules-and-analyze.md [blocked] — Regex custom rules, Swift custom rules (brief), swiftlint analyze, compiler-log workflow
  • SwiftLint documentation — Official docs
  • SwiftLint rule directory — Full categorized rule list
  • SimplyDanny/SwiftLintPlugins — Recommended plugin package

來源與署名

來源:dpearson2699/swift-ios-skills位於skills/swiftlint提交8d90fd1

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架

更多來自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指導實作、審查與改進 iOS、iPadOS、watchOS 與 CarPlay 上的 WidgetKit 小工具與控制項。

Software Development1.1K2 個月前更新

Weatherkit

dpearson2699

指導 iOS 開發者使用 WeatherService 取得 WeatherKit 預報、警報與署名資訊。

Software Development1.1K2 個月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分類1.1K2 個月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分類1.1K2 個月前更新

Tabletopkit

dpearson2699

指導使用 TabletopKit 在 visionOS 上打造多人空間桌遊,涵蓋棋具、座位、動作與 RealityKit 算繪。

Software Development1.1K2 個月前更新

Swiftui Webkit

dpearson2699

指導在 iOS 26 及更新版本的 SwiftUI App 中使用 WebKit for SwiftUI 嵌入與控制網頁內容。

Software Development1.1K2 個月前更新