Skip to content

Declare NoDiscard attribute as immutable#11611

Merged
danog merged 1 commit intovimeo:6.xfrom
Baptouuuu:6.x
Dec 23, 2025
Merged

Declare NoDiscard attribute as immutable#11611
danog merged 1 commit intovimeo:6.xfrom
Baptouuuu:6.x

Conversation

@Baptouuuu
Copy link
Contributor

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.

@psalm-github-bot
Copy link

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

@danog
Copy link
Collaborator

danog commented Dec 23, 2025

Thank you!

@danog danog merged commit 71ca5c4 into vimeo:6.x Dec 23, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants