Skip to content

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.

A scope has a file extension .swift, or a project file Package.swift.

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.

Run the fix: command that the report prints for the rules that fix themselves.

value as! NSString reports force_cast. Use as? and handle the failure, or change the input type so no cast remains.

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 literal

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.

Run the fix: command that the report prints.

func greeting reports consecutiveSpaces. Run gspot check --fix to get func greeting.

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 self

Builds 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.

Fix the first compiler error; later ones often come from the same mistake.

Assigning a string to an Int variable fails the build. Correct the value or the type.

gspot ignore swift/build --paths "<path>" --reason "<why>"

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.

Remove what the rule names. List an import that the analyzer flags in error under swift.allowed_unused_imports.

An import that no compiled Swift source uses reports a finding. Remove the import.

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 literal

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.

Delete the declaration, or mark it with a periphery:ignore comment that says who uses it.

A declaration that nothing references reports unused code. Delete it, and rebuild.

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 name

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.

Write why the test stays off and what turns it on again, or delete it.

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").

gspot ignore swift/skip-reasons --paths "<path>" --reason "<why>"

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.

Change the code the way the message says.

UserDefaults.standard.set(value, forKey: "accessToken") reports secret-in-userdefaults. Store the token in the Keychain.

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.
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.

Add tests for the target, or lower its minimum with a reason.

A Swift target reports 85% line coverage against a 90% minimum. Add a test that exercises its uncovered lines, then rerun the target.

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.
gspot ignore swift/tests --paths "<path>" --reason "<why>"
gspot ignore swift/tests --rule failed-test --reason "<why>"
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.
Setting Value
ci.operating_systems ["linux","macos"]
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.
Tool Version
swiftlint 0.65.1
swiftformat 0.64.0
periphery 3.8.0
swift Any, install it yourself
xcodebuild Any, install it yourself

.swiftlint.yml, **/*Tests/.swiftlint.yml, .gspot/configurations/swift/swiftlint.yml, .gspot/configurations/swift/swiftformat, and .gspot/configurations/swift/periphery.yml

.build, DerivedData, **/.build/**, **/DerivedData/**, and **/Pods/**. repository/local-files reports a tracked file at these paths.