Hi internals,

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.

On the other hand, applying this to all array_* functions is more
debatable. For example, functions like array_map() and array_filter()
can be used with callbacks that have side effects (impure
functions). In such cases, the return value might be intentionally
discarded. Applying #[NoDiscard] to them could lead to false positives
for legitimate code.

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
for existing code. This might be a type of change best reserved for a
major version like PHP 9.0 to avoid backward compatibility breaks.

Finally, I would love to hear the community's thoughts on the
timeline. Should we aim to add these attributes for PHP 8.5, or should
we introduce potentially breaking changes like these more gradually,
perhaps leading up to PHP 9.0?

Thanks,

Kenta

-- 
ヾ(〃><)ノ゙☆ Kenta Usami (宇佐美健太)
github.com/zonuexe ; https://tadsan.github.io/

Reply via email to