crystal-ameba / ameba

A static code analysis tool for Crystal
https://crystal-ameba.github.io
MIT License
514 stars 35 forks source link

ComparisonToBoolean with Bool unions #432

Closed Blacksmoke16 closed 8 months ago

Blacksmoke16 commented 8 months ago

Given the following code:

val = false || 10

if val == true
  pp "TRUE"
else
  pp val.to_s
end

This currently results in:

[W] Lint/ComparisonToBoolean: Comparison to a boolean is pointless
> if val == true
     ^---------^

However I think in this case it's a false positive since val : Bool | Int32, in which case you need the explicit comparison in order to not enter the TRUE block if val happens to be any number.

Sija commented 8 months ago

There's nothing that can be done here, short of introducing semantic analysis.

Sija commented 8 months ago

Duplicate of #171