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-11139] guard's else doesn't recognize variable == nil as validating non optional nil value in it #53535

Closed
swift-ci opened this issue Jul 15, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself feature A feature request or implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11139
Radar None
Original Reporter idomo (JIRA User)
Type Bug
Status Resolved
Resolution Won't Do
Environment

Swift 5, xCode 11.0 beta 3 (11M362v)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, LanguageFeatureRequest
Assignee idomo (JIRA)
Priority Medium

md5: 205361b48a935095ef57dba1655e44b7

Issue Description:

When writing something like:

guard error == nil else { return print(error) }

The error in the else, is assumed to be nil for the compiler and it gives me this warning:

Expression implicitly coerced from 'Error?' to 'Any'

Well, if I've checked that error is nil in the guard body, the else means that error != nil, so the value should be treated as an unwrapped value.

@belkadan
Copy link
Contributor

Swift doesn't do arbitrary type refinement like this, at least partially because it only works for immutable names. If you're working with a local variable, mutable class instance property, or mutable global, you wouldn't be able to rely on that refinement. Use if let here instead.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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. compiler The Swift compiler in itself feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

2 participants