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-6688] Misleading error message when using an object as a bool in an if statement #49237

Open
swift-ci opened this issue Jan 2, 2018 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 2, 2018

Previous ID SR-6688
Radar rdar://problem/18711616
Original Reporter mochs (JIRA User)
Type Bug
Environment

Swift 4

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: 6aface54cec0e448f76d567579e73b05

Issue Description:

This code is invalid in Swift (where `highlightView` is a `UILabel?`):

`if !highlightView {`

However the error the compiler shows is very misleading. The compiler says: "Optional type 'UILabel?' cannot be used as a boolean; test for '!= nil' instead". While this is true for the `highlightView` part, I think it is safe to say that many will instead think this is about the complete condition of the if statement. However the exact opposite should be checked in that case: `== nil`.

Also the fix it that is applied by the compiler changes the above expression to `if ![]((highlightView == nil) {which is also suboptimal, unusual and harder to read thanif highlightView )= nil {`.

I think this error and its fix it should be optimized in the case of a negated bool check.

@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. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant