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

php8's match: result of void is used #4292

Closed
jkuchar opened this issue Dec 29, 2020 · 10 comments
Closed

php8's match: result of void is used #4292

jkuchar opened this issue Dec 29, 2020 · 10 comments
Labels
Milestone

Comments

@jkuchar
Copy link

jkuchar commented Dec 29, 2020

Bug report

Even when result of match() is not used, phpstan reports that arm of match that contains fn which returns void is invalid. https://3v4l.org/Tcge3 (no error)

Code snippet that reproduces the problem

https://phpstan.org/r/aae87ebf-09e9-4bd1-a13b-2a39ff9af6f3

Expected output

PHPStan should ignore cases when match() result is not used, no matter what inner function returns.

@ondrejmirtes
Copy link
Member

I just fixed a similar bug today, feel free to send a PR with a fix for this one :) phpstan/phpstan-src@7ac6383

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Dec 29, 2020
@patrickkusebauch
Copy link

I just run into this issue. I would be careful about this one though. This seems like an undefined behaviour to me. It is not discussed in the official docs, it is not discussed in the RFC. For example, PHPStorm will report the same issue as PHPStan does.

The closest mention in the docs related to the issue is "A match expression returns a value" and "The result of a match expression does not need to be used". If we were to strictly adhere to this, the fact that the code above works is a bug in PHP.

@patrickkusebauch
Copy link

I have filed a bug report https://bugs.php.net/bug.php?id=80858. I denoted it as a documentation bug for now, as the inteded behavior is not clear to me.

@ondrejmirtes
Copy link
Member

It's not a PHP bug. In runtime void function actually returns null so handling the value is consistent with the rest of the language.

@simPod
Copy link
Contributor

simPod commented Mar 29, 2021

I tried with psalm https://psalm.dev/r/f7b57538bf and gives no error. How will phpstan approach this in the future? Is it safe to ignore error for now?

@jkuchar
Copy link
Author

jkuchar commented Mar 29, 2021

Ondrej wrote that this is considered bug and should be fixed. ref However I'm currently too busy to prepare PR. Feel free to prepare it, I will be happy to do pre-review or initial consulation.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@90e49f7

@jkuchar
Copy link
Author

jkuchar commented Mar 29, 2021

Thank you @ondrejmirtes !

@ondrejmirtes
Copy link
Member

FYI instead I wrote this rule: phpstan/phpstan-src@2c0dda3

@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 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants