Skip to content

Throwing an Error does not do anything? #15258

Closed
phpstan/phpstan-src
#6471

Description

@claudepache

Bug report

Most probably an unwanted side-effect of phpstan/phpstan-src@c7301f6. The following defensive code:

throw new \ValueError('You provided a nonsensical value');

makes now PHPStan complain:

Expression "throw new \ValueError('You provided a nonsensical value')" on a separate line does not do anything.

Code snippet that reproduces the problem

https://phpstan.org/r/597818aa-a7f9-4183-9278-1e87a1055698

Expected output

No error on line 12.

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. ondrejmirtes commented on Sep 17, 2026

    @ondrejmirtes
    Member

    Yeah I know, it's a bug I want to fix before release.

    This should be decided in MethodCallHandler, StaticCallHandler, FuncCallHandler, NewHandler.

    I'd be fine if ExpressionHandler continued to invoke NoopExpressionNode for all nodes that aren't the ones handled by the handlers listed above.

    And the handlers above could decide whether they want to invoke NoopExpressionNode based on whether the explicit throw point is about new ObjectType(Error::class) or not.

  2. added this to the Easy fixes milestone on Sep 17, 2026
  3. phpstan-bot commented on Sep 17, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -PHP 8.0 – 8.6 (1 error)
    -==========
    -
    -12: Expression "throw new \ValueError('cannot extract a prefix from a string that has no prefix')" on a separate line does not do anything.
    -
    -PHP 7.2 – 7.4 (3 errors)
    -==========
    -
    -12: Instantiated class ValueError not found.
    -12: Possibly impure instantiation of unknown class in pure function extract_prefix().
    -12: Throwing object of an unknown class ValueError.
    +12: Expression "throw new \ValueError('cannot extract a prefix from a string that has no prefix')" on a separate line does not do anything.
    Full report
    Line Error
    12 Expression "throw new \ValueError('cannot extract a prefix from a string that has no prefix')" on a separate line does not do anything.
  4. phpstan-bot commented on Sep 18, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#6471

  5. github-actions commented on Oct 20, 2026

    @github-actions

    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.

  6. locked as resolved and limited conversation to collaborators on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions