Re: [PATCH 1/2] echo: pacify Oracle Studio 12.6

2020-06-02 Thread Jim Meyering
On Mon, Jun 1, 2020 at 4:45 PM Bob Proulx  wrote:
> Paul Eggert wrote:
> > * src/echo.c (main): Don’t assign pointer to bool.
> > This is well-defined in C99, but is arguably bad style
> > and Oracle Studio 12.6 complains.
> ...
> > +  bool posixly_correct = !!getenv ("POSIXLY_CORRECT");
>
> Of course this is fine.  But because char *getenv() returns a pointer
> it just has this feeling of expressing frustration with the compiler
> seeing the !! there.  It feels like an obvious cast silencing a
> compiler warning.  Perhaps that is the intent? :-)
>
>   RETURN VALUE
>The getenv() function returns a pointer to the value in the
>environment, or NULL if there is no match.
>
> Just as a soft comment that isn't very strong if it were me I would
> use a comparison against a pointer which produced a boolean result and
> that boolean result used to assign to a bool.  It just feels to me
> like more of the types are more obvious this way.  And no cast of any
> type is either implicit or explicit.
>
>   bool posixly_correct = getenv ("POSIXLY_CORRECT") != NULL;

Hi Bob,

I had the same aversion to "!!", ... up until maybe a decade ago when
I realized we could view "!!" as a pointer-to-bool "operator".
It's an idiom that you have to use a few times before it starts to
become natural, then you won't want to go back.

Jim



Re: [PATCH 1/2] echo: pacify Oracle Studio 12.6

2020-06-01 Thread Bob Proulx
Paul Eggert wrote:
> * src/echo.c (main): Don’t assign pointer to bool.
> This is well-defined in C99, but is arguably bad style
> and Oracle Studio 12.6 complains.
...
> +  bool posixly_correct = !!getenv ("POSIXLY_CORRECT");

Of course this is fine.  But because char *getenv() returns a pointer
it just has this feeling of expressing frustration with the compiler
seeing the !! there.  It feels like an obvious cast silencing a
compiler warning.  Perhaps that is the intent? :-)

  RETURN VALUE
   The getenv() function returns a pointer to the value in the
   environment, or NULL if there is no match.

Just as a soft comment that isn't very strong if it were me I would
use a comparison against a pointer which produced a boolean result and
that boolean result used to assign to a bool.  It just feels to me
like more of the types are more obvious this way.  And no cast of any
type is either implicit or explicit.

  bool posixly_correct = getenv ("POSIXLY_CORRECT") != NULL;

But of course it's all a matter of style.

Bob



[PATCH 1/2] echo: pacify Oracle Studio 12.6

2020-06-01 Thread Paul Eggert
* src/echo.c (main): Don’t assign pointer to bool.
This is well-defined in C99, but is arguably bad style
and Oracle Studio 12.6 complains.
---
 src/echo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/echo.c b/src/echo.c
index b5a6e966c..e7f3447ee 100644
--- a/src/echo.c
+++ b/src/echo.c
@@ -108,7 +108,7 @@ int
 main (int argc, char **argv)
 {
   bool display_return = true;
-  bool posixly_correct = getenv ("POSIXLY_CORRECT");
+  bool posixly_correct = !!getenv ("POSIXLY_CORRECT");
   bool allow_options =
 (! posixly_correct
  || (! DEFAULT_ECHO_TO_XPG && 1 < argc && STREQ (argv[1], "-n")));
-- 
2.17.1