From: | Michael Paquier <michael(at)paquier(dot)xyz> |
---|---|
To: | Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com> |
Cc: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: warn_unused_results |
Date: | 2020-11-09 06:56:06 |
Message-ID: | 20201109065606.GA11424@paquier.xyz |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On Sat, Oct 17, 2020 at 08:57:51AM +0200, Peter Eisentraut wrote:
> Forgetting to assign the return value of list APIs such as lappend() is a
> perennial favorite. The compiler can help point out such mistakes. GCC has
> an attribute warn_unused_results. Also C++ has standardized this under the
> name "nodiscard", and C has a proposal to do the same [0]. In my patch I
> call the symbol pg_nodiscard, so that perhaps in a distant future one only
> has to do s/pg_nodiscard/nodiscard/ or something similar. Also, the name is
> short enough that it doesn't mess up the formatting of function declarations
> too much.
I have seen as well this stuff being a source of confusion in the
past.
> I have added pg_nodiscard decorations to all the list functions where I
> found it sensible, as well as repalloc() for good measure, since realloc()
> is usually mentioned as an example where this function attribute is useful.
+#ifdef __GNUC__
+#define pg_nodiscard __attribute__((warn_unused_result))
+#else
+#define pg_nodiscard
+#endif
This is accepted by clang, and MSVC has visibly an equivalent for
that, as of VS 2012:
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
#define pg_nodiscard _Check_return_
We don't care about the 1700 condition as we support only >= 1800 on
HEAD, and in this case the addition of pg_nodiscard would be required
on the definition and the declaration. Should it be added? It is
much more invasive than the gcc/clang equivalent though..
> I have found two places in the existing code where this creates warnings.
> Both places are correct as is, but make assumptions about the internals of
> the list APIs and it seemed better just to fix the warning than to write a
> treatise about why it's correct as is.
FWIW, I saw an extra case with parsePGArray() today. I am not sure
about the addition of repalloc(), as it is quite obvious that one has
to use its result. Lists are fine, these are proper to PG internals
and beginners tend to be easily confused in the way to use them.
--
Michael
From | Date | Subject | |
---|---|---|---|
Next Message | Peter Eisentraut | 2020-11-09 07:23:31 | Re: warn_unused_results |
Previous Message | Seino Yuki | 2020-11-09 06:39:05 | Re: [PATCH] Add features to pg_stat_statements |