Skip to content

Add new rule unnest_switches_using_tuple #6081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

snofla
Copy link

@snofla snofla commented May 17, 2025

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
}

@SwiftLintBot
Copy link

SwiftLintBot commented May 17, 2025

12 Warnings
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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)
⚠️ 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
📖 Building this branch resulted in a binary size of 25065.26 KiB vs 25052.16 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 1.04 s vs 1.04 s on main (0% slower).
📖 Linting Alamofire with this PR took 1.39 s vs 1.4 s on main (0% faster).
📖 Linting Brave with this PR took 9.57 s vs 9.58 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 23.86 s vs 24.04 s on main (0% faster).
📖 Linting Firefox with this PR took 11.37 s vs 11.38 s on main (0% faster).
📖 Linting Kickstarter with this PR took 10.71 s vs 10.76 s on main (0% faster).
📖 Linting Moya with this PR took 0.55 s vs 0.55 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 3.13 s vs 3.14 s on main (0% faster).
📖 Linting Nimble with this PR took 0.83 s vs 0.83 s on main (0% slower).
📖 Linting PocketCasts with this PR took 8.87 s vs 8.99 s on main (1% faster).
📖 Linting Quick with this PR took 0.47 s vs 0.47 s on main (0% slower).
📖 Linting Realm with this PR took 4.85 s vs 4.86 s on main (0% faster).
📖 Linting Sourcery with this PR took 2.46 s vs 2.46 s on main (0% slower).
📖 Linting Swift with this PR took 5.67 s vs 5.65 s on main (0% slower).
📖 Linting VLC with this PR took 1.45 s vs 1.46 s on main (0% faster).
📖 Linting Wire with this PR took 21.44 s vs 21.44 s on main (0% slower).
📖 Linting WordPress with this PR took 12.67 s vs 12.69 s on main (0% faster).

Generated by 🚫 Danger

@snofla snofla marked this pull request as draft May 17, 2025 12:57
@snofla snofla force-pushed the snofla/nested-switches branch from fd133d4 to f7dc976 Compare May 18, 2025 20:52
@snofla snofla marked this pull request as ready for review May 19, 2025 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants