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

Description

The #[\NoDiscard] attribute marks a function, method or closure so that the engine emits a warning whenever a caller ignores its return value. A void-returning function never produces a value to begin with, so requiring the caller to use one is a contradiction, and PHP rejects the combination at compile time.

Example

<?php

#[\NoDiscard]
function foo(): void {}

?>

Literal Examples

  • A void function does not return a value, but #[NoDiscard] requires a return value

  • A void method does not return a value, but #[NoDiscard] requires a return value

Solutions

  • Remove the void return type, and actually return a meaningful value.

  • Remove the #[\NoDiscard] attribute, since there is nothing to discard.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.