Re: RFC: Marking return values as important (#[\NoDiscard])

From: Date: Wed, 29 Jan 2025 20:16:11 +0000
Subject: Re: RFC: Marking return values as important (#[\NoDiscard])
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Wed, Jan 29, 2025, at 20:50, Tim Düsterhus wrote:
> Hi
> 
> On 1/29/25 20:31, Rob Landers wrote:
> > This looks promising!
> > 
> > Why do we need the (void) cast? Wouldn't "@" work and avoid a BC issue?
> 
> The @ operator also suppresses any other warning or notice within the 
> call chain. That is likely undesirable because it might hide issues one 
> is interested in. See this example (https://3v4l.org/NDtR7):
> 
>      <?php
>      function inner() {
>          var_dump($undefined);
>      }
> 
>      function outer() {
>          inner();
>      }
> 
>      outer();
>      outer();
>      @outer();
> 
> The (void) cast makes the intent of not being interested in the return 
> value clear. In C casting to (void) is an established pattern of 
> "using" a value that the compiler would otherwise consider unused. 
> Likewise will the void operator suppress the TypeScript-ESLint warning 
> of not using returned Promise: 
> https://typescript-eslint.io/rules/no-floating-promises/#ignorevoid
> 
> Best regards
> Tim Düsterhus
> 

Hi Tim,

I understand what you are saying, but I can also just remove the warning via:

$_ = outer;

Which, to me, is clearer on the intent than (void)outer(); It also happens to make the diffs more
natural looking when and if the return value gets used during a code review. I'll also note
that having (void) doesn't preclude me from using $_ either.

I was mostly just curious as to the reasoning; I don't like it, but it makes sense.

Best of luck!

— Rob


Thread (48 messages)

« previous php.internals (#126235) next »