Swiftlint: Update no_fallthrough_only rule condition to handle @unknown default

Created on 28 Mar 2019  路  3Comments  路  Source: realm/SwiftLint

In swift 4.2 and later we can use @unknown default in a switch block case to handle _future_ enum cases.
This is ok however, as far as I know, this case has to be handled separately from other cases in a switch.

Let's say that we have the following script:

#!/usr/bin/swift
import Foundation

enum EnumExample {
    case caseA, caseB
}

let enumInstance: EnumExample = .a

switch enumInstance {
case .caseA:
    print("it's a")
case .caseB:
    fallthrough
@unknown default:
    print("it's not a")
}

This works as expected, however, again, as far as I know, there's no way to merge the cases case .caseB: and @unknown default:.

As the latest SwiftLint version (0.31.0), this code triggers the no_fallthrough_only warning:

warning: No Fallthrough Only Violation: Fallthroughs can only be used if the `case` contains at least one other statement. (no_fallthrough_only)

Steps to Reproduce

  1. copy the code above in a empty .swift file
  2. make said file executable (if you want to run it) by running $ chmod +x YOURFILENAME.swift
  3. run $ swiftlint

Expected Results

no no_fallthrough_only warning

Actual Results

no_fallthrough_only warning

I understand why the warning triggers, however I believe this case where we use @unknown default should be an exception to this rule.

Thank you in advance for all the support! 馃

enhancement

Most helpful comment

Perhaps we could/should silence violations that fall through to an @unknown default case?

All 3 comments

Running into this same scenario, would greatly appreciate the proposed exception!

Perhaps we could/should silence violations that fall through to an @unknown default case?

Perhaps we could/should silence violations that fall through to an @unknown default case?

@jpsim I鈥檇 be ok with that 馃槉

Was this page helpful?
0 / 5 - 0 ratings