swift
Swift sources and their XCTest and Swift Testing tests: lint and analyzer rules for each level, one format, a clean build, no unused code, a reason on each disabled test, and passing tests, with a coverage minimum at level all. Guide: Swift.
Selected when
Section titled “Selected when”A scope has a file extension .swift, or a project file Package.swift.
Checks
Section titled “Checks”swiftlint
Section titled “swiftlint”Lints Swift files with the SwiftLint files of each scope and nested folder, and treats warnings as errors.
Runs at the commit stage, on macOS and Linux. gspot check --fix fixes what it can.
Forced casts and tries can crash, and a strong delegate can keep its owner alive.
What to do
Section titled “What to do”Run the fix: command that the report prints for the rules that fix themselves.
Example
Section titled “Example”value as! NSString reports force_cast. Use as? and handle the failure, or change the input type so no cast remains.
Turn it off
Section titled “Turn it off”gspot ignore swift/swiftlint --paths "<path>" --reason "<why>"gspot ignore swift/swiftlint --rule <rule> --reason "<why>"To silence one place, write a comment such as:
// swiftlint:disable:next force_unwrapping - reason: the URL is a literalswiftformat
Section titled “swiftformat”Checks the layout of every Swift file against the shipped SwiftFormat rules.
Runs at the commit stage, on macOS and Linux. gspot check --fix fixes what it can.
One layout everywhere keeps a diff about the change and not about where a brace went.
What to do
Section titled “What to do”Run the fix: command that the report prints.
Example
Section titled “Example”func greeting reports consecutiveSpaces. Run gspot check --fix to get func greeting.
Turn it off
Section titled “Turn it off”gspot ignore swift/swiftformat --paths "<path>" --reason "<why>"gspot ignore swift/swiftformat --rule <rule> --reason "<why>"To silence one place, write a comment such as:
// swiftformat:disable:next redundantSelf - reason: the closure needs selfBuilds the package or Xcode scheme once per scope, and reports the compiler errors and the warnings in your own sources. A package build treats warnings as errors.
Runs at the push stage, once per scope, on macOS and Linux, and needs a build and network access.
Swift checks types only during a build.
What to do
Section titled “What to do”Fix the first compiler error; later ones often come from the same mistake.
Example
Section titled “Example”Assigning a string to an Int variable fails the build. Correct the value or the type.
Turn it off
Section titled “Turn it off”gspot ignore swift/build --paths "<path>" --reason "<why>"swiftlint-analyze
Section titled “swiftlint-analyze”Runs the SwiftLint analyzer rules over the compiler log of the build: unused imports and captured variables.
Runs only when gspot check --only swift/swiftlint-analyze names it, once per scope, on macOS, and needs a build and network access.
An unused import slows the build and hides the real dependencies, and a captured variable can keep an object alive or read a stale value.
What to do
Section titled “What to do”Remove what the rule names. List an import that the analyzer flags in error under swift.allowed_unused_imports.
Example
Section titled “Example”An import that no compiled Swift source uses reports a finding. Remove the import.
Turn it off
Section titled “Turn it off”gspot ignore swift/swiftlint-analyze --paths "<path>" --reason "<why>"To silence one place, write a comment such as:
// swiftlint:disable:next force_unwrapping - reason: the URL is a literalperiphery
Section titled “periphery”Scans the built project for declarations nothing uses.
Runs at the push stage, once per scope, on macOS, and needs a build and network access.
People read, maintain, and migrate dead code without knowing that nothing uses it.
What to do
Section titled “What to do”Delete the declaration, or mark it with a periphery:ignore comment that says who uses it.
Example
Section titled “Example”A declaration that nothing references reports unused code. Delete it, and rebuild.
Turn it off
Section titled “Turn it off”gspot ignore swift/periphery --paths "<path>" --reason "<why>"To silence one place, write a comment such as:
// periphery:ignore - reason: the runtime calls it by nameskip-reasons
Section titled “skip-reasons”Checks that skip calls and disabled traits name a nonempty reason, and unavailable tests carry an explanation.
Runs at the commit stage.
A skipped test with no reason stays off, because nobody can tell when to restore it.
What to do
Section titled “What to do”Write why the test stays off and what turns it on again, or delete it.
Example
Section titled “Example”throw XCTSkip() in a Swift test reports a missing reason. Restore the test, or give a reason such as throw XCTSkip("Requires the hardware test device").
Turn it off
Section titled “Turn it off”gspot ignore swift/skip-reasons --paths "<path>" --reason "<why>"security
Section titled “security”Finds always-accessible Keychain items, MD5 and SHA-1 hashes, each secret in UserDefaults, a plist read, or a log, and SQLite calls whose SQL text interpolates a value.
Runs at the commit stage.
A secret in plain storage or a log outlives the app that wrote it, and a broken hash protects nothing.
What to do
Section titled “What to do”Change the code the way the message says.
Example
Section titled “Example”UserDefaults.standard.set(value, forKey: "accessToken") reports secret-in-userdefaults. Store the token in the Keychain.
Lint rules
Section titled “Lint rules”| Rule | What it checks | What to do |
|---|---|---|
keychain-accessible-always |
No Keychain item uses kSecAttrAccessibleAlways. |
Use kSecAttrAccessibleWhenUnlocked, or a stricter class that fits the item. |
weak-hash-algorithm |
No code hashes with MD5 or SHA-1. | Switch to SHA256 or SHA512 of CryptoKit. |
secret-in-log |
No log call writes a token, password, secret, or credential in public. | Leave the value out of the log, or mark it with privacy: .private. |
secret-in-userdefaults |
No code stores a token, password, secret, or credential in UserDefaults. |
Save the value with the Keychain Services API. |
secret-in-plist |
No code reads a secret, password, or private key from Info.plist. |
Fetch the secret at run time, and keep it in the Keychain. |
sql-interpolation |
No SQLite call runs SQL text built by string interpolation. | Write ? in the SQL text, and pass each value with a sqlite3_bind function. |
Turn it off
Section titled “Turn it off”gspot ignore swift/security --paths "<path>" --reason "<why>"gspot ignore swift/security --rule keychain-accessible-always --reason "<why>"Measures each source target in Xcode projects and Swift packages against coverage.min_line_percent and its coverage.swift_target minimum.
Runs at the push stage, once per scope, on macOS and Linux, and needs a build and network access.
A coverage minimum for each target exposes Swift code that no test runs.
What to do
Section titled “What to do”Add tests for the target, or lower its minimum with a reason.
Example
Section titled “Example”A Swift target reports 85% line coverage against a 90% minimum. Add a test that exercises its uncovered lines, then rerun the target.
Lint rules
Section titled “Lint rules”| Rule | What it checks | What to do |
|---|---|---|
failed-test |
Each Swift test passes. | Fix the code or the test at the reported line. |
coverage |
Each target covers at least coverage.min_line_percent of its lines, or the limit of its [[coverage.swift_target]] record. |
Add tests for the lines that no test runs. |
Turn it off
Section titled “Turn it off”gspot ignore swift/tests --paths "<path>" --reason "<why>"gspot ignore swift/tests --rule failed-test --reason "<why>"Settings
Section titled “Settings”| Setting | What it sets |
|---|---|
coverage.min_line_percent |
The minimum percentage of source lines covered by tests. |
coverage.swift_target |
Line coverage minimums for named Swift targets. |
swift.xcode_project |
The Xcode project or workspace, which init fills in when it finds one; without it, the checks use the Swift package. |
swift.xcode_scheme |
The shared scheme the build, the analyzer, and Periphery use. |
swift.allowed_unused_imports |
Imports the analyzer keeps although it sees no use of them. |
swift.xcode_destination |
The -destination that xcodebuild builds for. |
Defaults set for other configurations
Section titled “Defaults set for other configurations”| Setting | Value |
|---|---|
ci.operating_systems |
["linux","macos"] |
Rules for coding agents
Section titled “Rules for coding agents”| Rule file | Installed when |
|---|---|
.gspot/rules/SWIFT-TESTS.md |
The repository has a Swift test file, or a Package.swift with a test target. |
.gspot/rules/SWIFT.md |
Always |
.gspot/rules/SWIFTUI.md |
The repository has a Swift file that imports SwiftUI. |
.gspot/rules/UIKIT.md |
The repository has a Swift file that imports UIKit. |
Tools and versions
Section titled “Tools and versions”| Tool | Version |
|---|---|
| swiftlint | 0.65.1 |
| swiftformat | 0.64.0 |
| periphery | 3.8.0 |
| swift | Any, install it yourself |
| xcodebuild | Any, install it yourself |
Files gspot writes
Section titled “Files gspot writes”.swiftlint.yml, **/*Tests/.swiftlint.yml, .gspot/configurations/swift/swiftlint.yml, .gspot/configurations/swift/swiftformat, and .gspot/configurations/swift/periphery.yml
Paths that must stay out of Git
Section titled “Paths that must stay out of Git”.build, DerivedData, **/.build/**, **/DerivedData/**, and **/Pods/**. repository/local-files reports a tracked file at these paths.