← All results

PHP 8.5 #[\NoDiscard]: discarding the return value is a warning, and (void) is the documented opt-out at one call site

Group: Analyzer directives · Category: extension · File: directives_nodiscard_return.php

A discarded call of an otherwise identical function *without* the attribute is the control: a diagnostic there means the reaction is generic unused-return lint, not the attribute. Assignment, echo, and (void) must stay silent. PHP does not inherit the attribute onto an implementation; this file only probes a concrete function that carries it itself. Source: vimeo/psalm#11892 (6.18.0). PHPStan already had the rule; 2.2.15 dropped its PHP-version gate. Phan documents PhanNoDiscardReturnValueIgnored. References: - https://wiki.php.net/rfc/marking_return_value_as_important - psalm#11892

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
directives_nodiscard_return.php:41: Return value of \Conformance\Tests\DirectivesNodiscardReturn\mintId() with #[\NoDiscard] attribute is discarded - use (void) cast to suppress this warning [PhanNoDiscardReturnValueIgnored]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass reported Lv.0+
directives_nodiscard_return.php:41: Call to function Conformance\Tests\DirectivesNodiscardReturn\mintId() on a separate line discards return value. [reported-from-level=0]
With strict-rules
directives_nodiscard_return.php:41: Call to function Conformance\Tests\DirectivesNodiscardReturn\mintId() on a separate line discards return value.
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass (pzoom≠)
directives_nodiscard_return.php:41: The call to conformance\tests\directivesnodiscardreturn\mintid is not used [UnusedFunctionCall]
pzoom (Psalm port)

No diagnostics from pzoom.

mago 1.57.0mago 1.57.0 Pass
directives_nodiscard_return.php:41: The return value of 'Conformance\Tests\DirectivesNodiscardReturn\mintId' must be used. [unused-function-call]
mir 0.87.0mir 0.87.0 Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
phpantom 0.11.1phpantom_lsp 0.11.1 Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Pass
directives_nodiscard_return.php:41: The return value of mintId() is expected to be consumed. [P1126]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
phpy 1.0.19409phpy 1.0.19409 Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 41: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
directives_nodiscard_return.php:41: Call to function Conformance\Tests\DirectivesNodiscardReturn\mintId() on a separate line discards return value.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Pass
directives_nodiscard_return.php:41: The call to conformance\tests\directivesnodiscardreturn\mintid is not used [UnusedFunctionCall]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\DirectivesNodiscardReturn;

/**
 * PHP 8.5 `#[\NoDiscard]`: discarding the return value is a warning, and
 * `(void)` is the documented opt-out at one call site.
 *
 * A discarded call of an otherwise identical function *without* the attribute
 * is the control: a diagnostic there means the reaction is generic unused-return
 * lint, not the attribute. Assignment, `echo`, and `(void)` must stay silent.
 *
 * PHP does not inherit the attribute onto an implementation; this file only
 * probes a concrete function that carries it itself.
 *
 * Source: vimeo/psalm#11892 (6.18.0). PHPStan already had the rule; 2.2.15
 * dropped its PHP-version gate. Phan documents `PhanNoDiscardReturnValueIgnored`.
 *
 * References:
 * - https://wiki.php.net/rfc/marking_return_value_as_important
 * - https://github.com/vimeo/psalm/pull/11892
 */

#[\NoDiscard] // E?[noise]: unknown-attribute class on tools without PHP 8.5 stubs
function mintId(): int // E?[noise]: same, blamed on the signature
{
    echo 'side-effect';

    return 1;
}

function ordinaryId(): int
{
    echo 'side-effect';

    return 1;
}

mintId(); // E: discarded #[\NoDiscard] return is a PHP 8.5 warning
echo mintId(); // V: using the return is the point of the attribute
(void) mintId(); // Q: (void) is the documented per-call opt-out
ordinaryId(); // V: without the attribute, discarding an impure return is fine