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

Improvements to switch/case exhaustiveness checking #3488

Open
Firehed opened this issue Jun 16, 2020 · 8 comments
Open

Improvements to switch/case exhaustiveness checking #3488

Firehed opened this issue Jun 16, 2020 · 8 comments

Comments

@Firehed
Copy link
Contributor

Firehed commented Jun 16, 2020

Feature request

When using the const-as-enum pattern (supported in 0.12.20 via #2904), it would be really helpful to improve several warnings. Turning on strict rules does handle explicitly invalid cases (IMO that should be in the default rules, but that's a different conversation), but defaults get a bit weird:

  • Exhausting all switch cases should arguably mark the default case as unreachable
  • If all cases have been covered, the lack of a default should not result in a "method xyz should return type but return statement is missing" error
  • It would be helpful if the error for case-mismatch was on the case line rather than switch

https://phpstan.org/r/e3457146-1c41-4e70-9740-fe5d67f7f987

@Firehed Firehed changed the title Improvements to enum exhaustiveness checking Improvements to switch/case exhaustiveness checking Jun 16, 2020
@ondrejmirtes
Copy link
Member

The third one was easy: phpstan/phpstan-strict-rules@82c19b1

@phpstan-bot
Copy link
Contributor

@Firehed PHPStan now reports different result with your code snippet:

@@ @@
 15: Method C::allCovered() should return int but return statement is missing.
 27: Method C::invalidCase() should return int but return statement is missing.
-27: Switch condition type ('a'|'b'|'c') does not match case condition 'd' (string).
+31: Switch condition type ('a'|'b'|'c') does not match case condition 'd' (string).
Full report
Line Error
15 Method C::allCovered() should return int but return statement is missing.
27 Method C::invalidCase() should return int but return statement is missing.
31 `Switch condition type ('a'

@devbanana
Copy link

Adding my vote to this one. doesn't make sense to require a default in these cases.

@phpstan-bot
Copy link
Contributor

@Firehed After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-15: Method C::allCovered() should return int but return statement is missing.
-27: Method C::invalidCase() should return int but return statement is missing.
-27: Switch condition type ('a'|'b'|'c') does not match case condition 'd' (string).
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

@MidnightDesign
Copy link
Contributor

Somewhere between pre-1.0 and now @phpstan-ignore-next-line stopped working for these cases: https://phpstan.org/r/e1b07d56-dfdc-4f31-b2fe-3ca21b0a94f3

@peldax
Copy link

peldax commented Jan 5, 2023

I am current experiencing false positive related to second point you are describing.

Minimal playground: https://phpstan.org/r/b967b249-3d2b-4ed5-9cbb-eadd88777a2c

@ondrejmirtes
Copy link
Member

@peldax Your example got fixed: phpstan/phpstan-src#2687

@phpstan-bot
Copy link
Contributor

@peldax After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.1 – 8.2 (1 error)
+PHP 8.1 – 8.2
 ==========
 
-11: Function testFunction() should return int but return statement is missing.
+No errors
 
 PHP 7.2 – 8.0 (1 error)
 ==========
 
  3: Syntax error, unexpected T_STRING on line 3
Full report

PHP 8.1 – 8.2

No errors

PHP 7.2 – 8.0 (1 error)

Line Error
3 Syntax error, unexpected T_STRING on line 3

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

6 participants