Introduction
SwiftLint checks Swift code against a set of style rules derived from the community Swift style guide. It integrates into Xcode build phases, CI pipelines, and editors to catch style violations and potential bugs before code review.
What SwiftLint Does
- Analyzes Swift source files against 200+ configurable lint rules
- Auto-corrects fixable violations with the
--fixflag - Supports custom rules defined via regular expressions in
.swiftlint.yml - Integrates as an Xcode build phase to show warnings inline
- Reports results in multiple formats including JSON, HTML, and Xcode-compatible output
Architecture Overview
SwiftLint is written in Swift and uses SourceKit to parse Swift source files into an abstract syntax tree. Each rule is implemented as a self-contained module that inspects the AST or raw token stream. The configuration layer reads .swiftlint.yml to enable, disable, or customize rules per project or directory.
Self-Hosting & Configuration
- Install via Homebrew (
brew install swiftlint), Mint, CocoaPods, or download the binary from GitHub releases - Create a
.swiftlint.ymlfile in your project root to configure enabled/disabled rules - Add a Run Script build phase in Xcode to run SwiftLint on each build
- Set
excludedpaths to skip generated code or third-party dependencies - Configure
custom_ruleswith regex patterns for project-specific conventions
Key Features
- Over 200 built-in rules covering style, naming, metrics, and potential bugs
- Auto-correction for many violations with
swiftlint --fix - Per-directory configuration with nested
.swiftlint.ymlfiles - Inline rule suppression using
// swiftlint:disablecomments - Support for analyzing Swift Package Manager projects and Xcode workspaces
Comparison with Similar Tools
- SwiftFormat — focuses purely on formatting and code style normalization; SwiftLint covers broader lint categories including complexity and naming
- swift-format (Apple) — the official Apple formatter with fewer rules; SwiftLint offers more community-driven rules
- Periphery — detects unused code rather than style violations; complementary to SwiftLint
- Tailor — an older Swift linter that is no longer actively maintained
FAQ
Q: Can I use SwiftLint with Swift Package Manager projects?
A: Yes. Run swiftlint from the package root and it will analyze all Swift files. You can also add it as a build tool plugin.
Q: How do I suppress a specific rule for one line?
A: Add // swiftlint:disable:next rule_name on the line above, or wrap a section with // swiftlint:disable rule_name and // swiftlint:enable rule_name.
Q: Does SwiftLint work in CI environments without macOS? A: SwiftLint requires SourceKit, which is bundled with the Swift toolchain. It runs on macOS and Linux where Swift is installed.
Q: How do I create custom rules?
A: Define them in .swiftlint.yml under custom_rules with a regex, message, and severity. For complex logic, write a native rule as a Swift module.