SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Add new rule `unnest_switches_using_tuple`

Open snofla opened this issue 8 months ago • 1 comments

This introduces a new rule that detects nested switches that reference the same variable. This prevents needless indentation.

The canonical example is:

switch a {
case .one:
    switch b {
    case .one:
        break
    case .two:
        break
    }
case .two:
    switch b {
    case .one:
        break
    case .two:
        break
    }
}

A better suggestion in this case is to switch on a tuple. (While the general suggestion would be to move the switch to a separate function.)

switch (a, b) {
case (.one, .one):
    break
case (.one, .two):
    break
case (.two, .one):
    break
case (.two, .two):
    break
}

snofla avatar May 17 '25 12:05 snofla

12 Warnings
:warning: This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveStore/Subscription/SDK/BraveStoreSDK.swift:37:5: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/Bookmarks/AddEditBookmarkTableViewController.swift:281:5: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in DuckDuckGo: /macOS/DuckDuckGo/SecureVault/Model/SecureVaultSorting.swift:97:13: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in DuckDuckGo: /macOS/DuckDuckGo/DefaultBrowserAndAddToDockPrompts/DefaultBrowserAndDockPromptContent.swift:59:9: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in DuckDuckGo: /macOS/DuckDuckGo/DefaultBrowserAndAddToDockPrompts/DefaultBrowserAndDockPromptContent.swift:79:9: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in DuckDuckGo: /iOS/DuckDuckGo/OnboardingExperiment/BrowsersComparison/BrowsersComparisonModel.swift:33:13: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in DuckDuckGo: /iOS/DuckDuckGo/Refactoring/Updated/ButtonConfigurations/ToolbarButton.swift:85:9: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in Moya: /Tests/MoyaTests/Error+MoyaSpec.swift:8:13: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in Wire: /wire-ios-request-strategy/Sources/Payloads/Processing/ConversationEventPayloadProcessor.swift:914:9: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in Wire: /WireDomain/Sources/WireDomain/Repositories/Conversations/LocalStore/ConversationLocalStore+MLS.swift:55:9: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in WordPress: /WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsViewModel.swift:451:13: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
:warning: This PR introduced a violation in WordPress: /Modules/Sources/DesignSystem/Typography/FontDescriptor.swift:47:9: Warning: Prevent nesting switches by preferring a switch on a tuple (unnest_switches_using_tuple)
18 Messages
:book: Building this branch resulted in a binary size of 25065.26 KiB vs 25052.16 KiB when built on main (0% larger).
:book: Linting Aerial with this PR took 1.04 s vs 1.04 s on main (0% slower).
:book: Linting Alamofire with this PR took 1.39 s vs 1.4 s on main (0% faster).
:book: Linting Brave with this PR took 9.57 s vs 9.58 s on main (0% faster).
:book: Linting DuckDuckGo with this PR took 23.86 s vs 24.04 s on main (0% faster).
:book: Linting Firefox with this PR took 11.37 s vs 11.38 s on main (0% faster).
:book: Linting Kickstarter with this PR took 10.71 s vs 10.76 s on main (0% faster).
:book: Linting Moya with this PR took 0.55 s vs 0.55 s on main (0% slower).
:book: Linting NetNewsWire with this PR took 3.13 s vs 3.14 s on main (0% faster).
:book: Linting Nimble with this PR took 0.83 s vs 0.83 s on main (0% slower).
:book: Linting PocketCasts with this PR took 8.87 s vs 8.99 s on main (1% faster).
:book: Linting Quick with this PR took 0.47 s vs 0.47 s on main (0% slower).
:book: Linting Realm with this PR took 4.85 s vs 4.86 s on main (0% faster).
:book: Linting Sourcery with this PR took 2.46 s vs 2.46 s on main (0% slower).
:book: Linting Swift with this PR took 5.67 s vs 5.65 s on main (0% slower).
:book: Linting VLC with this PR took 1.45 s vs 1.46 s on main (0% faster).
:book: Linting Wire with this PR took 21.44 s vs 21.44 s on main (0% slower).
:book: Linting WordPress with this PR took 12.67 s vs 12.69 s on main (0% faster).

Generated by :no_entry_sign: Danger

SwiftLintBot avatar May 17 '25 12:05 SwiftLintBot