psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Declare `NoDiscard` attribute as immutable

Open Baptouuuu opened this issue 4 weeks ago • 1 comments

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.

Baptouuuu avatar Dec 10 '25 12:12 Baptouuuu

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

psalm-github-bot[bot] avatar Dec 10 '25 12:12 psalm-github-bot[bot]