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

[Feature Request] Relax Lint/ComparisonToBoolean to allow unions with Bool #171

Closed
HertzDevil opened this issue Nov 1, 2020 · 1 comment

Comments

@HertzDevil
Copy link

HertzDevil commented Nov 1, 2020

Comparisons to booleans are not pointless if the other operand is a union containing Bool:

def f(x : Int32 | Bool)
  print 1 if x == true
  print 2 if x == false
  print 3 if x
  print 4 if !x
  puts
end

f(0)     # 3
f(true)  # 13
f(false) # 24

(Comparisons to unions not containing Bool are still pointless, as they should always evaluate to false.)

@veelenga
Copy link
Member

veelenga commented Feb 3, 2021

In Crystal comparison to booleans can be used to narrow down the type of the variable, thus doing the static analysis, we are not able to properly handle this. Refs #12

def f(x: Bool | Nil)
  if x == false
    # x is not Int32 and not Nil here.
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants