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

compare of a float against 0 should error #6843

Closed
staabm opened this issue Mar 16, 2022 · 4 comments
Closed

compare of a float against 0 should error #6843

staabm opened this issue Mar 16, 2022 · 4 comments

Comments

@staabm
Copy link
Contributor

staabm commented Mar 16, 2022

Bug report

see https://3v4l.org/Nlt7t
related issue thecodingmachine/safe#286

Code snippet that reproduces the problem

https://phpstan.org/r/b2a1e65b-14ad-43de-82c3-4219635bce90

Expected output

error, because comparison of a float against int-0 always leads to true/false
atm we see only a single error

@ondrejmirtes
Copy link
Member

"always true" is only reported with strict-rules (https://phpstan.org/r/be2789f5-5ea2-40bf-97cc-0fe9ba25dee8) or this option enabled: https://phpstan.org/config-reference#checkalwaystruestrictcomparison (check other similar options around that one in the documentation)

@herndlm
Copy link
Contributor

herndlm commented Mar 16, 2022

Not entirely sure what the best phpstan behaviour would be here. I think it's still weird. Maybe report nothing?

But an identical/equal comparison of floats with anything is always problematic, right? Does it make sense to report this in a rule? I guess the only correct thing to do is smth like abs($foo) <= PHP_FLOAT_EPSILON or so for the 0 comparison .
I think, what I'm suggesting is, to make all == and === and their inversions/false contexts with floats error. Does that make sense?

@ondrejmirtes
Copy link
Member

to make all == and === and their inversions/false contexts with floats error. Does that make sense?

There's an old PR about this with some discussion: phpstan/phpstan-strict-rules#38

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants