Hi
Am 2025-07-01 16:41, schrieb Kenta Usami:
> I'm writing to start a discussion about expanding the use of the newly
> accepted #[NoDiscard] attribute.
> I believe this attribute is a fantastic addition that will help
> prevent many subtle bugs, and we can make it even more effective by
> applying it to a broader set of core functions.
>
> My initial thoughts for strong candidates are:
>
> - All is_* prefixed functions
> - The password_*() functions
> - Most string functions: Many string functions like strlen(),
> substr(), trim(), and str_replace() are also clear candidates.
> We would naturally exclude functions with output side effects, such
> as the printf() family.
> - Mathematical functions
> - Pure array_*() functions: Functions that don't accept callbacks,
> such as array_keys(), array_values(), array_reverse(), and
> array_unique(), are also strong candidates. Their return value is
> their only result.
As one of the RFC authors, adding `#[\NoDiscard]` to every pure function
was an explicit non-goal of the RFC. Please let me refer to:
https://wiki.php.net/rfc/marking_return_value_as_important#recommended_usage
The goal of the attribute is to prevent
hard-to-detect-but-easy-to-create bugs. Forgetting to use the return
value of `str_contains()` is not particularly easy to do
unintentionally.
> This nuance extends to other areas, like many filesystem functions
> which require case-by-case evaluation. A tricky example is
> file_get_contents(). At first glance, it seems like an obvious
> candidate. However, despite its name, it can be used to send external
> HTTP requests where the primary goal might be the side effect of the
> request itself (like a webhook ping), and the returned content is
> intentionally ignored. Adding #[NoDiscard] here could create issues
`file_get_contents()` on the other hand might be a good candidate, since
even for a “ping” request you are generally interested in finding out
whether or not the request succeeded. However `file_get_contents()`
already emits warnings and notices in this case (and would probably
throw an Exception if it was newly introduced).
Best regards
Tim Düsterhus