Error Identifier: callable.inVoidCast
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
$fn = function (): int {
return 42;
};
(void) $fn();
Why is it reported? #
The (void) cast (PHP 8.5+) is used to explicitly discard a return value of a callable that does not require its return value to be used. The (void) cast is intended only for callables marked with #[NoDiscard] where the return value must normally be used. Using (void) on a callable that already allows discarding its return value is unnecessary and misleading.
How to fix it #
Remove the (void) cast since the callable already allows discarding the return value:
<?php declare(strict_types = 1);
$fn = function (): int {
return 42;
};
-(void) $fn();
+$fn();
Or, if the callable’s return value should not be discarded, add the #[NoDiscard] attribute to the callable:
<?php declare(strict_types = 1);
+#[\NoDiscard]
function compute(): int {
return 42;
}
// Now (void) is meaningful - it explicitly discards a must-use return value
(void) compute();
How to ignore this error #
You can use the identifier callable.inVoidCast to ignore this error using a comment:
// @phpstan-ignore callable.inVoidCast
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: callable.inVoidCast
Rules that report this error #
- PHPStan\Rules\Functions\CallToFunctionStatementWithNoDiscardRule [1]