Skip to content

MIR1514

NoDiscardReturnIgnored

A function or method marked #[\NoDiscard] is called as a statement on its own, so its return value is thrown away. From PHP 8.5 this emits a warning. Builtins such as DateTimeImmutable::add() carry the attribute because calling them only for their side effects is a bug.

<?php
function shift(DateTimeImmutable $date): DateTimeImmutable {
$date->add(new DateInterval('P1D')); // returns a new object; $date is unchanged
return $date;
}

Use the return value, or cast the call to (void) when discarding it is intended:

<?php
function shift(DateTimeImmutable $date): DateTimeImmutable {
return $date->add(new DateInterval('P1D'));
}