Skip to content

Commit

Permalink
Add FXIOS-6020 [v113] Swiftlint: for_where (#13807)
Browse files Browse the repository at this point in the history
  • Loading branch information
adudenamedruby authored Mar 31, 2023
1 parent ce763b2 commit a456a77
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 133 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ disabled_rules: # rule identifiers to exclude from running
- duplicated_key_in_dictionary_literal
- fallthrough
- file_length
- for_where
- force_cast
- force_try
- function_body_length
Expand Down
4 changes: 2 additions & 2 deletions Client/Extensions/UIView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ extension UIView {
}

func removeVisualEffectView() {
for subview in self.subviews {
if subview is UIVisualEffectView { subview.removeFromSuperview() }
for subview in self.subviews where subview is UIVisualEffectView {
subview.removeFromSuperview()
}
// Set clipsToBounds to false to make sure shadows will be visible
clipsToBounds = false
Expand Down
6 changes: 2 additions & 4 deletions Client/Frontend/Browser/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,8 @@ class SearchViewController: SiteTableViewController,

func searchRemoteTabs(for searchString: String) {
filteredRemoteClientTabs.removeAll()
for remoteClientTab in remoteClientTabs {
if remoteClientTab.tab.title.lowercased().contains(searchQuery) {
filteredRemoteClientTabs.append(remoteClientTab)
}
for remoteClientTab in remoteClientTabs where remoteClientTab.tab.title.lowercased().contains(searchQuery) {
filteredRemoteClientTabs.append(remoteClientTab)
}

let currentTabs = self.remoteClientTabs
Expand Down
16 changes: 6 additions & 10 deletions Client/Frontend/Browser/String+Punycode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,19 @@ extension String {
return self
}
var labels = self.components(separatedBy: ".")
for (i, part) in labels.enumerated() {
if !isValidUnicodeScala(part) {
let a = encode(part)
labels[i] = prefixPunycode + a
}
for (i, part) in labels.enumerated() where !isValidUnicodeScala(part) {
let a = encode(part)
labels[i] = prefixPunycode + a
}
let resultString = labels.joined(separator: ".")
return resultString
}

public func asciiHostToUTF8() -> String {
var labels = self.components(separatedBy: ".")
for (index, part) in labels.enumerated() {
if isValidPunycodeScala(part) {
let changeStr = String(part[part.index(part.startIndex, offsetBy: 4)...])
labels[index] = decode(changeStr)
}
for (index, part) in labels.enumerated() where isValidPunycodeScala(part) {
let changeStr = String(part[part.index(part.startIndex, offsetBy: 4)...])
labels[index] = decode(changeStr)
}
let resultString = labels.joined(separator: ".")
return resultString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ struct LegacyWallpaperManager {
// If no wallpaper was ever set, then we must be at index 0
guard let currentWallpaper = storageManager.getCurrentWallpaperObject() else { return 0 }

for (index, wallpaper) in dataManager.availableWallpapers.enumerated() {
if wallpaper == currentWallpaper { return index }
for (index, wallpaper) in dataManager.availableWallpapers.enumerated() where wallpaper == currentWallpaper {
return index
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ class DownloadsPanelViewModel {
}

func isFirstSection(_ section: Int) -> Bool {
for index in 0..<section {
if hasDownloadedItem(for: index) {
return false
}
for index in 0..<section where hasDownloadedItem(for: index) {
return false
}
return true
}
Expand Down
7 changes: 3 additions & 4 deletions Client/Frontend/Library/ReaderPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ class ReadingListTableViewCell: UITableViewCell, ThemeApplicable {

fileprivate func simplifiedHostnameFromURL(_ url: URL) -> String {
let hostname = url.host ?? ""
for prefix in prefixesToSimplify {
if hostname.hasPrefix(prefix) {
return String(hostname[hostname.index(hostname.startIndex, offsetBy: prefix.count)...])
}
for prefix in prefixesToSimplify where hostname.hasPrefix(prefix) {
return String(hostname[hostname.index(hostname.startIndex, offsetBy: prefix.count)...])
}

return hostname
}

Expand Down
8 changes: 4 additions & 4 deletions Client/TabManagement/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -805,11 +805,11 @@ class Tab: NSObject {
guard let url = self.webView?.url else {
return .none
}
for provider in SearchEngine.allCases {
if url.absoluteString.contains(provider.rawValue) {
return provider
}

for provider in SearchEngine.allCases where url.absoluteString.contains(provider.rawValue) {
return provider
}

return .none
}
}
Expand Down
41 changes: 20 additions & 21 deletions Client/Utils/SearchTermGroupsUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,29 @@ class SearchTermGroupsUtility {
var itemsInGroups: [T] = [T]()

outeritemLoop: for item in items {
innerMetadataLoop: for (searchTerm, historyMetaList) in searchTermMetadata {
if historyMetaList.contains(where: { metadata in
var stringURL: String = ""

if let item = item as? Site {
stringURL = item.url
} else if let item = item as? Tab, let url = item.lastKnownUrl?.absoluteString {
stringURL = url
} else if let item = item as? HistoryHighlight {
stringURL = item.url
}

return metadata.url == stringURL || metadata.referrerUrl == stringURL
}) {
itemsInGroups.append(item)
if itemGroupData[searchTerm] == nil {
itemGroupData[searchTerm] = [item]
} else {
itemGroupData[searchTerm]?.append(item)
}
break innerMetadataLoop
innerMetadataLoop: for (searchTerm, historyMetaList) in searchTermMetadata where historyMetaList.contains(where: { metadata in
var stringURL: String = ""

if let item = item as? Site {
stringURL = item.url
} else if let item = item as? Tab, let url = item.lastKnownUrl?.absoluteString {
stringURL = url
} else if let item = item as? HistoryHighlight {
stringURL = item.url
}

return metadata.url == stringURL || metadata.referrerUrl == stringURL
}) {
itemsInGroups.append(item)
if itemGroupData[searchTerm] == nil {
itemGroupData[searchTerm] = [item]
} else {
itemGroupData[searchTerm]?.append(item)
}
break innerMetadataLoop
}
}

return (itemGroupData, itemsInGroups)
}

Expand Down
8 changes: 3 additions & 5 deletions Providers/RustSyncManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,9 @@ public class RustSyncManager: NSObject, SyncManager {
// Massage the list of names into engine identifiers.var engines = [String]()
var engines = [String]()

for name in names {
// There may be duplicates in `names` so we are removing them here
if !engines.contains(name) {
engines.append(name)
}
// There may be duplicates in `names` so we are removing them here
for name in names where !engines.contains(name) {
engines.append(name)
}

// Ensuring that only valid engines are submitted
Expand Down
18 changes: 6 additions & 12 deletions Shared/Extensions/Array+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ public extension Array where Element: Comparable {

public extension Array {
func find(_ f: (Iterator.Element) -> Bool) -> Iterator.Element? {
for x in self {
if f(x) {
return x
}
for x in self where f(x) {
return x
}
return nil
}

func contains(_ x: Element, f: (Element, Element) -> Bool) -> Bool {
for y in self {
if f(x, y) {
return true
}
for y in self where f(x, y) {
return true
}
return false
}
Expand Down Expand Up @@ -72,10 +68,8 @@ public extension Sequence where Iterator.Element: Hashable {

public extension Sequence {
func every(_ f: (Self.Iterator.Element) -> Bool) -> Bool {
for x in self {
if !f(x) {
return false
}
for x in self where !f(x) {
return false
}
return true
}
Expand Down
6 changes: 2 additions & 4 deletions Shared/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,8 @@ public func mapValues<K, T, U>(_ source: [K: T], f: (T) -> U) -> [K: U] {
}

public func findOneValue<K, V>(_ map: [K: V], f: (V) -> Bool) -> V? {
for v in map.values {
if f(v) {
return v
}
for v in map.values where f(v) {
return v
}
return nil
}
Expand Down
6 changes: 2 additions & 4 deletions Shared/NSUserDefaultsPrefs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ open class NSUserDefaultsPrefs: Prefs {
open func clearAll() {
// TODO: userDefaults.removePersistentDomainForName() has no effect for app group suites.
// iOS Bug? Iterate and remove each manually for now.
for key in userDefaults.dictionaryRepresentation().keys {
if key.hasPrefix(prefixWithDot) {
userDefaults.removeObject(forKey: key)
}
for key in userDefaults.dictionaryRepresentation().keys where key.hasPrefix(prefixWithDot) {
userDefaults.removeObject(forKey: key)
}
}
}
10 changes: 3 additions & 7 deletions Storage/SQL/BrowserSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,15 @@ open class BrowserSchema: Schema {

// TODO: transaction.
func run(_ db: SQLiteDBConnection, queries: [(String, Args?)]) -> Bool {
for (sql, args) in queries {
if !run(db, sql: sql, args: args) {
for (sql, args) in queries where !run(db, sql: sql, args: args) {
return false
}
}
return true
}

func run(_ db: SQLiteDBConnection, queries: [String]) -> Bool {
for sql in queries {
if !run(db, sql: sql) {
return false
}
for sql in queries where !run(db, sql: sql) {
return false
}
return true
}
Expand Down
6 changes: 2 additions & 4 deletions Storage/SQL/ReadingListSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ open class ReadingListSchema: Schema {
}

func run(_ db: SQLiteDBConnection, queries: [String]) -> Bool {
for sql in queries {
if !run(db, sql: sql, args: nil) {
return false
}
for sql in queries where !run(db, sql: sql, args: nil) {
return false
}
return true
}
Expand Down
27 changes: 11 additions & 16 deletions Sync/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,25 @@ open class Scratchpad {
// Default bundle has changed. Reset everything but collections that have unchanged bulk keys.
var except: Set<String> = Set()
// Symmetric difference, like an animal. Swift doesn't allow Hashable tuples; don't fight it.
for (collection, keyBundle) in staleKeys.collectionKeys {
if keyBundle == freshKeys.forCollection(collection) {
except.insert(collection)
}
for (collection, keyBundle) in staleKeys.collectionKeys where keyBundle == freshKeys.forCollection(collection) {
except.insert(collection)
}
for (collection, keyBundle) in freshKeys.collectionKeys {
if keyBundle == staleKeys.forCollection(collection) {
except.insert(collection)
}

for (collection, keyBundle) in freshKeys.collectionKeys where keyBundle == staleKeys.forCollection(collection) {
except.insert(collection)
}
self.localCommands.insert(.resetAllEngines(except: except))
} else {
// Default bundle is the same. Reset collections that have changed bulk keys.
for (collection, keyBundle) in staleKeys.collectionKeys {
if keyBundle != freshKeys.forCollection(collection) {
self.localCommands.insert(.resetEngine(engine: collection))
}
for (collection, keyBundle) in staleKeys.collectionKeys where keyBundle != freshKeys.forCollection(collection) {
self.localCommands.insert(.resetEngine(engine: collection))
}
for (collection, keyBundle) in freshKeys.collectionKeys {
if keyBundle != staleKeys.forCollection(collection) {
self.localCommands.insert(.resetEngine(engine: collection))
}

for (collection, keyBundle) in freshKeys.collectionKeys where keyBundle != staleKeys.forCollection(collection) {
self.localCommands.insert(.resetEngine(engine: collection))
}
}

return self
}

Expand Down
8 changes: 3 additions & 5 deletions Tests/SharedTests/FeatureSwitchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ class FeatureSwitchTests: XCTestCase {
let prefs = MockProfilePrefs()
var membership = featureSwitch.isMember(prefs)
var changed = 0
for _ in 0..<100 {
if featureSwitch.isMember(prefs) != membership {
membership = !membership
changed += 1
}
for _ in 0..<100 where featureSwitch.isMember(prefs) != membership {
membership = !membership
changed += 1
}

XCTAssertEqual(changed, 0, "Users should get and keep the feature over restarts")
Expand Down
11 changes: 5 additions & 6 deletions content-blocker-lib-ios/src/ContentBlocker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,12 @@ extension ContentBlocker {
}

let blocklists = BlocklistFileName.allCases.map { $0.filename }
for listOnDisk in blocklists {
// If any file from the list on disk is not installed, remove all the rules and re-install them
if !available.contains(where: { $0 == listOnDisk}) {
noMatchingIdentifierFoundForRule = true
break
}
// If any file from the list on disk is not installed, remove all the rules and re-install them
for listOnDisk in blocklists where !available.contains(where: { $0 == listOnDisk}) {
noMatchingIdentifierFoundForRule = true
break
}

if !noMatchingIdentifierFoundForRule {
completion()
return
Expand Down
Loading

0 comments on commit a456a77

Please sign in to comment.