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

[SR-14189] Warn when list of patterns in case statement is structurally similar to pattern being matched #56567

Open
swift-ci opened this issue Feb 11, 2021 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14189
Radar None
Original Reporter jumhyn (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 6f36f24aeff06a0cc92dd9c3ea11be21

Issue Description:

Consider the following example:

func check(_ arg1: Int?, _ arg2: Int?) -> Bool {
    switch (arg1, arg2) {
    case (.some(let val1), .some(let val2)):
        return val1 > val2
    case nil, _:
        return true
    }
}

The `case nil, _:` case appears to cover cases where `arg1` is `nil` and `arg2` is anything. However, because `nil` and `_` are treated as two separate patterns, this will actually cover all values for `arg1` and `arg2` (which is why exhaustivity checking doesn't complain).

We may want to offer a warning here (along with the appropriate fix-its) that warns the user that they are not matching against `(nil, _)` as it might appear. The warning could be silenced by writing:

    case nil:
        fallthrough
    case _:
        ...
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

1 participant