Skip to content
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

optional_enum_case_matching false positive #3057

Closed
2 tasks done
allen-zeng opened this issue Jan 28, 2020 · 2 comments · Fixed by #3088
Closed
2 tasks done

optional_enum_case_matching false positive #3057

allen-zeng opened this issue Jan 28, 2020 · 2 comments · Fixed by #3088
Labels
bug Unexpected and reproducible misbehavior.

Comments

@allen-zeng
Copy link
Contributor

allen-zeng commented Jan 28, 2020

New Issue Checklist

Describe the bug

The rule picks up the pattern _?. This is incorrect, especially when the pattern _? is used to distinguish between nil and non-nil.

Complete output when running SwiftLint, including the stack trace and command used
$ echo "import Foundation enum Test {     case a, b, c } let x: Test? = .c let y: Test? = nil switch (x, y) { case (.c, _?):     print(\"this shouldn't print\") case (.c, nil):     print(\"this is the correct print\") case (_, _):     print(\"this is to make the compiler happy\") }" | swiftlint lint --no-cache --use-stdin --enable-all-rules
Loading configuration from '.swiftlint.yml'
<nopath>:1:29: warning: Closure Spacing Violation: Closure expressions should have a single space inside each brace. (closure_spacing)
<nopath>:1:19: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
<nopath>:1:50: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
<nopath>:1:68: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
<nopath>:1:19: warning: Explicit Top Level ACL Violation: Top-level declarations should specify Access Control Level keywords explicitly. (explicit_top_level_acl)
<nopath>:1:50: warning: Explicit Top Level ACL Violation: Top-level declarations should specify Access Control Level keywords explicitly. (explicit_top_level_acl)
<nopath>:1:68: warning: Explicit Top Level ACL Violation: Top-level declarations should specify Access Control Level keywords explicitly. (explicit_top_level_acl)
<nopath>:1:40: error: Identifier Name Violation: Enum element name should be between 3 and 40 characters long: 'a' (identifier_name)
<nopath>:1:43: error: Identifier Name Violation: Enum element name should be between 3 and 40 characters long: 'b' (identifier_name)
<nopath>:1:46: error: Identifier Name Violation: Enum element name should be between 3 and 40 characters long: 'c' (identifier_name)
<nopath>:1:50: error: Identifier Name Violation: Variable name should be between 3 and 40 characters long: 'x' (identifier_name)
<nopath>:1:68: error: Identifier Name Violation: Variable name should be between 3 and 40 characters long: 'y' (identifier_name)
<nopath>:1:1: error: Line Length Violation: Line should be 120 characters or less: currently 268 characters (line_length)
<nopath>:1:114: 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)
<nopath>:1:54: warning: Prefixed Top-Level Constant Violation: Top-level constants should be prefixed by `k`. (prefixed_toplevel_constant)
<nopath>:1:72: warning: Prefixed Top-Level Constant Violation: Top-level constants should be prefixed by `k`. (prefixed_toplevel_constant)
<nopath>:1:103: warning: Switch and Case Statement Alignment Violation: Case statements should vertically align with their enclosing switch statement. (switch_case_alignment)
<nopath>:1:152: warning: Switch and Case Statement Alignment Violation: Case statements should vertically align with their enclosing switch statement. (switch_case_alignment)
<nopath>:1:207: warning: Switch and Case Statement Alignment Violation: Case statements should vertically align with their enclosing switch statement. (switch_case_alignment)
<nopath>:1:103: warning: Switch Case on Newline Violation: Cases inside a switch should always be on a newline (switch_case_on_newline)
<nopath>:1:152: warning: Switch Case on Newline Violation: Cases inside a switch should always be on a newline (switch_case_on_newline)
<nopath>:1:207: warning: Switch Case on Newline Violation: Cases inside a switch should always be on a newline (switch_case_on_newline)
Done linting! Found 22 violations, 6 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)?
    Homebrew and CocoaPods, ran both
  • Paste your configuration file:
opt_in_rules:
- optional_enum_case_matching
  • Are you using nested configurations?
    No
  • Which Xcode version are you using (check xcodebuild -version)?
    Xcode 11.3.1
    Build version 11C504
  • Do you have a sample that shows the issue?
import Foundation

enum Test {
    case a, b, c
}

let x: Test? = .c
let y: Test? = nil

switch (x, y) {
// If we remove the `?`, the compiler will warn that the second `case` is already covered by this, and the output will be `this shouldn't print`.
case (.c, _?): 
    print("this shouldn't print")
case (.c, nil):
    print("this is the correct print")
case (_, _):
    print("this is to make the compiler happy")
}
@marcelofabri marcelofabri added the bug Unexpected and reproducible misbehavior. label Jan 28, 2020
@marcelofabri
Copy link
Collaborator

@allen-zeng do you want to give this a shot? shouldn't be too hard to fix

@allen-zeng
Copy link
Contributor Author

I sure can but I won't be able to immediately. If anyone else gets a chance definitely jump in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants