From 02bee41067ab2822cbffcb4b3b2387f79488dffd Mon Sep 17 00:00:00 2001 From: sasezaki Date: Fri, 27 Feb 2026 23:49:08 +0900 Subject: [PATCH] Add NoDiscard attribute (#5041) Co-authored-by: Gina Peter Banyard --- language/predefined/attributes.xml | 1 + language/predefined/attributes/nodiscard.xml | 177 ++++++++++++++++++ .../attributes/nodiscard/construct.xml | 53 ++++++ language/predefined/versions.xml | 2 + 4 files changed, 233 insertions(+) create mode 100644 language/predefined/attributes/nodiscard.xml create mode 100644 language/predefined/attributes/nodiscard/construct.xml diff --git a/language/predefined/attributes.xml b/language/predefined/attributes.xml index 3ae9cd9b7a..3e97de7913 100644 --- a/language/predefined/attributes.xml +++ b/language/predefined/attributes.xml @@ -11,6 +11,7 @@ &language.predefined.attributes.attribute; &language.predefined.attributes.allowdynamicproperties; &language.predefined.attributes.deprecated; + &language.predefined.attributes.nodiscard; &language.predefined.attributes.override; &language.predefined.attributes.returntypewillchange; &language.predefined.attributes.sensitiveparameter; diff --git a/language/predefined/attributes/nodiscard.xml b/language/predefined/attributes/nodiscard.xml new file mode 100644 index 0000000000..f02b83c763 --- /dev/null +++ b/language/predefined/attributes/nodiscard.xml @@ -0,0 +1,177 @@ + + + The NoDiscard attribute + NoDiscard + + + +
+ &reftitle.intro; + + This attribute can be used to indicate that the return value of a function + or a method should not be discarded. If the return value is not used in any + way, a warning will be emitted. + + + This is useful for functions where not checking the return value is likely + to be a bug. + + + To intentionally discard the return value of such a function, use (void) + cast to suppress the warning. + + + + Since attributes are designed to be backwards compatible, + #[\NoDiscard] can be added to functions and methods + even when PHP 8.4 or below are supported, it just won't do anything. + On PHP 8.5 and above a warning will be emitted if the result is unused. + To suppress the warning without using (void), + which is not supported before PHP 8.5, + consider using a variable like $_. + + +
+ +
+ &reftitle.classsynopsis; + + + + final + NoDiscard + + + &Properties; + + public + readonly + stringnull + message + + + &Methods; + + + + + +
+ +
+ &reftitle.properties; + + + message + + + An optional message explaining why the return value should not be discarded. + + + + +
+ +
+ &reftitle.examples; + + Basic usage + + $items + * @return array + */ +#[\NoDiscard("as processing might fail for individual items")] +function bulk_process(array $items): array { + $results = []; + + foreach ($items as $key => $item) { + if (\random_int(0, 9999) < 9999) { + // Pretend to do something useful with $item, + // which will succeed in 99.99% of cases. + echo "Processing {$item}", PHP_EOL; + $error = null; + } else { + $error = new \Exception("Failed to process {$item}."); + } + + $results[$key] = $error; + } + + return $results; +} + +bulk_process($items); + +?> +]]> + + &example.outputs.85.similar; + + + + + + Intentionally discarding the return value + + +]]> + + +
+ +
+ &reftitle.seealso; + + Attributes overview + +
+ +
+ + &language.predefined.attributes.nodiscard.construct; + +
+ diff --git a/language/predefined/attributes/nodiscard/construct.xml b/language/predefined/attributes/nodiscard/construct.xml new file mode 100644 index 0000000000..408741fdbc --- /dev/null +++ b/language/predefined/attributes/nodiscard/construct.xml @@ -0,0 +1,53 @@ + + + + + NoDiscard::__construct + Construct a new NoDiscard attribute instance + + + + &reftitle.description; + + public NoDiscard::__construct + stringnullmessage&null; + + + Constructs a new NoDiscard instance. + + + + + &reftitle.parameters; + + + message + + + The value of the message property. + + + + + + + diff --git a/language/predefined/versions.xml b/language/predefined/versions.xml index 70f57a4404..3a821aaa8e 100644 --- a/language/predefined/versions.xml +++ b/language/predefined/versions.xml @@ -183,6 +183,8 @@ + +