- Omit the return keyword in one-line functions that return a value.
- Use trailing closure syntax.
- Remove the Google indentation guide rule due to lack of specific guidelines in the Google Swift style guide and because SwiftLint handles indentation.
- Reevaluate the rule regarding DispatchQueue from inaction on the main thread for feasibility in static analysis.
- Use higher-order functions when multiple properties call the same function.
- Require every method to have a comment.
- Remove one-line functions that do not return anything and call the code directly.
- Avoid print statements; use a logging function instead (as a warning).
- Avoid function declarations within other function bodies.
- Use higher-order functions for processing arrays instead of loops.
- Avoid using
[String: Any]
; useCodable
instead. - Combine nested if-else statements into a single initialization statement.
- Avoid empty files.
- Inline constants or variables used only once.
- Google Indentation Guide: Removed because there is no specific section on indentation in the Google Swift style guide, and SwiftLint already handles indentation.
- DispatchQueue from inaction is already on main thread: Reevaluate the feasibility of enforcing this rule statically. Consider dynamic analysis or runtime checks to ensure code runs on the correct thread.
Non-triggering examples:
func blocks() -> [Block] {
.all(with: .retained, times: 2)
}
func fetchUser() -> User {
User(name: "John")
}
func calculateSum(a: Int, b: Int) -> Int {
a + b
}
Triggering examples:
func blocks() -> [Block] { return .all(with: .retained, times: 2) }
func fetchUser() -> User { return User(name: "John") }
func calculateSum(a: Int, b: Int) -> Int { return a + b }
Non-triggering examples:
someFunction { value in
print(value)
}
UIView.animate(withDuration: 0.3) {
self.view.alpha = 1.0
}
fetchData { result in
handle(result)
}
Triggering examples:
someFunction(settingsAction: { value in
print(value)
})
UIView.animate(withDuration: 0.3, animations: {
self.view.alpha = 1.0
})
fetchData(completion: { result in
handle(result)
})
Non-triggering examples:
[am6Btn, am7Btn].forEach { $0.titleLabel?.setAkinFont() }
[btn1, btn2, btn3].forEach { $0.isEnabled = true }
[view1, view2, view3].forEach { $0.isHidden = false }
Triggering examples:
am6Btn.titleLabel?.setAkinFont()
am7Btn.titleLabel?.setAkinFont()
btn1.isEnabled = true
btn2.isEnabled = true
btn3.isEnabled = true
view1.isHidden = false
view2.isHidden = false
view3.isHidden = false
More examples:
addButton.addTarget(
self,
action: #selector(addProfilePicPress),
for: .touchUpInside
)
addProfilePicButton.addTarget(
self,
action: #selector(addProfilePicPress),
for: .touchUpInside
)
Should be:
[addButton, addProfilePicButton].forEach {
$0.addTarget(self, action: #selector(addProfilePicPress), for: .touchUpInside)
}
logoutLabel.text = "Logout"
logoutLabel.textColor = .romanceRed
contentView.addSubview(logoutLabel)
logoutLabel.constraint(from: .centeredHorizontallyWith(contentView))
logoutLabel.constraint(from: .centeredVerticallyTo(contentView))
logoutLabel.constraint(from: .distanceToBottom(contentView.bottomAnchor, 36))
logoutLabel.constraint(from: .distanceToTop(contentView.topAnchor, 36))
Should be:
logoutLabel.text = "Logout"
logoutLabel.textColor = .romanceRed
contentView.addSubview(logoutLabel)
[.centeredHorizontallyWith(contentView), .centeredVerticallyTo(contentView),
.distanceToBottom(contentView.bottomAnchor, 36), .distanceToTop(contentView.topAnchor, 36)].forEach {
logoutLabel.constraint(from: $0)
}
rightAddButton.titleLabel?.setAkinFont()
scrollButton.titleLabel?.setAkinFont()
closeButton.titleLabel?.setAkinFont()
closeButtonBordered.titleLabel?.setAkinFont()
Should be:
[rightAddButton, scrollButton, closeButton, closeButtonBordered].forEach {
$0.titleLabel?.setAkinFont()
}
Non-triggering examples:
/// Fetches data from the server.
func fetchData() {
// implementation
}
/// Handles user login.
func loginUser() {
// implementation
}
Triggering examples:
func fetchData() {
// implementation
}
func loginUser() {
// implementation
}
Non-triggering examples:
print("Hello, World!")
Triggering examples:
func printHello() {
print("Hello, World!")
}
func reset() {
value = 0
}
Non-triggering examples:
logger.log("This is a log message")
Log.info("User logged in successfully")
debugPrint("Debugging information")
Triggering examples:
print("This is a log message")
print("User logged in successfully")
print("Debugging information")
Non-triggering examples:
func outerFunction() {
// code
}
func innerFunction() {
// code
}
func calculate() {
let result = compute()
// other code
}
func performAction() {
// action code
}
Triggering examples:
func outerFunction() {
func innerFunction() {
// code
}
innerFunction()
}
func calculate() {
func compute() -> Int {
return 5
}
let result = compute()
}
func performAction() {
func action() {
// action code
}
action()
}
Non-triggering examples:
let values = ["a", "b", "c"].map { $0.uppercased() }
let filtered = numbers.filter { $0 > 10 }
let sum = numbers.reduce(0, +)
Triggering examples:
var values = [String]()
for item in ["a", "b", "c"] {
values.append(item.uppercased())
}
var filtered = [Int]()
for number in numbers {
if number > 10 {
filtered.append(number)
}
}
var sum = 0
for number in numbers {
sum += number
}
Non-triggering examples:
struct MyData: Codable {
let name: String
let age: Int
}
let jsonData = try? JSONEncoder().encode(myData)
let myData = try? JSONDecoder().decode(MyData.self, from: jsonData)
Triggering examples:
let data: [String: Any] = ["name": "John", "age": 30]
let jsonData = try? JSONSerialization.data(withJSONObject: data)
let data = try? JSONSerialization.jsonObject(with: jsonData)
Non-triggering examples:
self.init(
presentation: enabled ? .enabled(importance ?? .irrelevant) : .disabled(importance),
auto
changedImportance: enabled && importance != nil
)
Triggering examples:
if enabled {
if let importance = importance {
self.init(presentation: .enabled(importance), autoChangedImportance: false)
} else {
self.init(presentation: .enabled(.irrelevant), autoChangedImportance: true)
}
} else {
self.init(presentation: .disabled(importance), autoChangedImportance: false)
}
Non-triggering examples:
- Any file with content.
Triggering examples:
- Any file that is completely empty.
Non-triggering examples:
alertViewController.styleCancelAlert(
regularContentsModel: RegularContentsView.Model(
title: "Are you sure?",
message: "If you close this greet, it can't be reopened."
),
models: UIButton.SimpleModel(
title: "Yes close it.",
action: {
self?.refuseGreetAndClose()
}
)
)
Triggering examples:
let contents = RegularContentsView.Model(
title: "Are you sure?",
message: "If you close this greet, it can't be reopened."
)
let buttonModel = UIButton.SimpleModel(
title: "Yes close it.",
action: {
self?.refuseGreetAndClose()
}
)
alertViewController.styleCancelAlert(
regularContentsModel: contents,
models: buttonModel
)
Non-triggering examples:
alertViewController.styleCancelAlert(
regularContentsModel: RegularContentsView.Model(
title: "Are you sure?",
message: "If you close this greet, it can't be reopened."
),
models: UIButton.SimpleModel(
title: "Yes close it.",
action: {
self?.refuseGreetAndClose()
}
)
)
Triggering examples:
let contents = RegularContentsView.Model(
title: "Are you sure?",
message: "If you close this greet, it can't be reopened."
)
let buttonModel = UIButton.SimpleModel(
title: "Yes close it.",
action: {
self?.refuseGreetAndClose()
}
)
alertViewController.styleCancelAlert(
regularContentsModel: contents,
models: buttonModel
)