realm / realm/SwiftLint

optional_enum_case_matching triggers on non-enum switch

Open
#3,044 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Describe the bug

The optional_enum_case_matching rule is triggered when switching over an optional struct with cases that are static members of the struct. In these instances, removing the ? on the case results in code that doesn't compile (Xcode 11.1).

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint --use-stdin --enable-all-rules
/// Test struct
public struct Foo: Equatable {
    public static let bar: Foo = .init(foo: "bar")

    public let foo: String
}

/// Test variable
public var something: Foo?

switch something {
case .bar?:
    print("bar")

default:
    print("default")
}
<nopath>:12:10: warning: Optional Enum Case Match Violation: Matching an enum case against an optional enum without '?' is supported on Swift 5.1 and above.  (optional_enum_case_matching)
Done linting! Found 1 violation, 0 serious in 1 file.
Environment
  • SwiftLint version (run swiftlint version to be sure)?
    0.38.2
  • Installation method used (Homebrew, CocoaPods, building from source, etc)?
    CocoaPods
  • Paste your configuration file
    n/a
  • Are you using nested configurations?
    If so, paste their relative paths and respective contents.
    n/a
  • Which Xcode version are you using (check xcodebuild -version)?
    Xcode 11.1, Build version 11A1027
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
    see above

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with swiftlint lint --use-stdin --enable-all-rules using the provided optional Foo struct example, then trace the optional_enum_case_matching rule's handling of switch cases. Done means the rule no longer reports this non-enum case while continuing to identify valid optional enum-case matches; add regression coverage for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.