Skip to content

[Diagnostics] Improve diagnostics for optional/integer as boolean condition #29628

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Feb 4, 2020

Conversation

owenv
Copy link
Contributor

@owenv owenv commented Feb 4, 2020

let x: Int = 0
if x {} // 1
if !x {} // 2

let y: String? = nil
if y {} // 3
if !y {} // 4

Used to give a good diagnostic for 3 and an ok one for 4. Now it results in the following:

hello.swift:3:4: error: type 'Int' cannot be used as a boolean; test for '!= 0' instead
if x {}
   ^
   ( != 0)
hello.swift:4:5: error: type 'Int' cannot be used as a boolean; test for '== 0' instead
if !x {}
   ~^
    ( == 0)
hello.swift:8:4: error: optional type 'String?' cannot be used as a boolean; test for '!= nil' instead 
if y {}
   ^
   ( != nil)
hello.swift:9:5: error: optional type 'String?' cannot be used as a boolean; test for '== nil' instead
if !y {}
   ~^
    ( == nil)

Resolves a FIXME and SR-4932

@owenv
Copy link
Contributor Author

owenv commented Feb 4, 2020

@swift-ci please smoke test

@owenv owenv requested a review from xedin February 4, 2020 02:28
@owenv
Copy link
Contributor Author

owenv commented Feb 4, 2020

@swift-ci please smoke test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you!

@xedin xedin merged commit 956b9f2 into swiftlang:master Feb 4, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants