Skip to content

Commit 8c86f49

Browse files
mokagioclaude
andauthored
Enable SwiftLint rule: empty_string (#25467)
* Enable SwiftLint rule: empty_string Part of the Orchard SwiftLint rollout campaign. `NSString.wp_isValidString()` uses `length > 0` instead of `!isEmpty` because `isEmpty` is not directly available on `NSString` in this context. --- Generated with the help of Claude Code, https://claude.ai/code Co-Authored-By: Claude Code Opus 4.6 <noreply@anthropic.com> * Fix optional chaining on non-optional String `blog.version` is non-optional, so `?.isEmpty` causes a compile error. --- Generated with the help of Claude Code, https://code.claude.com Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Clarify `empty_string` rule --------- Co-authored-by: Claude Code Opus 4.6 <noreply@anthropic.com>
1 parent e9e8600 commit 8c86f49

12 files changed

Lines changed: 14 additions & 11 deletions

File tree

.swiftlint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ only_rules:
3636
# Prefer `() -> ` over `Void -> `.
3737
- empty_parameters
3838

39+
# Prefer checking `isEmpty` over comparing to empty string literal `""`.
40+
- empty_string
41+
3942
# MARK comment should be in valid format.
4043
- mark
4144

Modules/Sources/WordPressKitModels/ObjectValidation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ import Foundation
1616
///
1717
/// - Returns: Bool value
1818
func wp_isValidString() -> Bool {
19-
return wp_isValidObject() && self != ""
19+
return wp_isValidObject() && length > 0
2020
}
2121
}

Sources/WordPressAuthenticator/Extensions/WPStyleGuide+Login.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ extension WPStyleGuide {
273273

274274
let labelString = NSMutableAttributedString(string: firstPart, attributes: [.foregroundColor: WPStyleGuide.greyDarken30()])
275275

276-
if lastPart != "" {
276+
if !lastPart.isEmpty {
277277
labelString.append(formattedGoogleString(forHyperlink: true))
278278
}
279279

Tests/KeystoneTests/Helpers/JSONObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension JSONObject {
1010
/// - Parameter fileName: The name of the json file to load. The "json" file extension can be omitted.
1111
init(fromFileNamed fileName: String) throws {
1212
let type = (fileName as NSString).pathExtension
13-
if type != "" && type != "json" {
13+
if !type.isEmpty && type != "json" {
1414
throw NSError(
1515
domain: "JSONObject",
1616
code: 1,

Tests/KeystoneTests/Tests/Utility/RemoteFeatureFlagTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RemoteFeatureFlagTests: XCTestCase {
1818
exp.expectedFulfillmentCount = 2
1919

2020
mock.deviceIdCallback = {
21-
deviceId == "" ? deviceId = $0 : XCTAssertEqual(deviceId, $0)
21+
deviceId.isEmpty ? deviceId = $0 : XCTAssertEqual(deviceId, $0)
2222
exp.fulfill()
2323
}
2424

Tests/WordPressDataTests/BlogTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ struct BlogTests {
202202
.build()
203203

204204
#expect((blog.version as Any) is String)
205-
#expect(blog.version == "")
205+
#expect(blog.version.isEmpty)
206206
}
207207

208208
@Test func removeDuplicates() async throws {

Tests/WordPressDataTests/PostMetadataTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct PostMetadataTests {
7979
metadata.encode(in: &container)
8080

8181
// THEN
82-
#expect(container.getString(for: .jetpackNewsletterEmailDisabled) == "")
82+
#expect(container.getString(for: .jetpackNewsletterEmailDisabled)?.isEmpty == true)
8383
}
8484
}
8585

WordPress/Classes/ViewRelated/Aztec/ViewControllers/AztecAttachmentViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class AztecAttachmentViewController: UITableViewController {
222222
}
223223

224224
@objc func handleDoneButtonTapped(sender: UIBarButtonItem) {
225-
let checkedAlt = alt == "" ? nil : alt
225+
let checkedAlt = alt?.isEmpty == true ? nil : alt
226226
onUpdate?(alignment, size, linkURL, checkedAlt, caption)
227227
dismiss(animated: true)
228228
}

WordPress/Classes/ViewRelated/CustomPostTypes/CustomPostListViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ final class CustomPostListViewModel: ObservableObject {
406406
func menuNavigation(forBlaze post: AnyPostWithEditContext) -> PostMenuNavigation? {
407407
guard endpoint == .posts
408408
&& BlazeHelper.isBlazeFlagEnabled() && blog.canBlaze
409-
&& post.status == .publish && (post.password ?? "") == "" else { return nil }
409+
&& post.status == .publish && (post.password ?? "").isEmpty else { return nil }
410410
return .blaze(post: post)
411411
}
412412

WordPress/Classes/ViewRelated/NUX/Controllers/Social Signup/SignupEpilogueViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private extension SignupEpilogueViewController {
194194
}
195195

196196
func changeUsername(to newUsername: String, finished: @escaping (() -> Void)) {
197-
guard newUsername != "" else {
197+
guard !newUsername.isEmpty else {
198198
finished()
199199
return
200200
}

0 commit comments

Comments
 (0)