psalm
psalm copied to clipboard
Declare `NoDiscard` attribute as immutable
Problem
When using the new NoDiscard attribute on a named constructor of a immutable class and that this named constructor is declared pure then Psalm 6.14 returns an error ImpureMethodCall on the attribute.
Example code: https://psalm.dev/r/1c176ef8bd
Fix
Declare the NoDiscard class as immutable.
Note
I tried creating a test (by looking at the OverrideTest as an example) but it errors with The class NoDiscard doesn't have the Attribute attribute. So far I haven't had the time to investigate how to fix that.
I found these snippets:
https://psalm.dev/r/1c176ef8bd
<?php
/**
* @psalm-immutable
*/
final class Box
{
private function __construct(
private mixed $value,
) {
}
/**
* @psalm-pure
*/
#[\NoDiscard]
public static function of(mixed $value): self
{
return new self($value);
}
}
Psalm output (using commit cdceda0):
INFO: UndefinedAttributeClass - 16:7 - Attribute class NoDiscard does not exist