Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A never returning %s does not return a value, but #[\NoDiscard] requires a return value

Description

This definition include contradicting information: the return type never means that the function will never return, so there should be no return value to handle. On the other hand, the NoDiscard attribute request the caller to do something with the returned values. This is irreconcilable.

Example

<?php

    #[\NoDiscard]
    function test(): never {
    	throw new \Exception('Error!');
    }

test();

?>

Literal Examples

  • A never returning function does not return a value, but #[\NoDiscard] requires a return value
  • A never returning method does not return a value, but #[\NoDiscard] requires a return value

Alternatives

  • Remove the never return type.
  • Remove the NoDiscard attribute.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See