C++ PSA: Use [[nodiscard]] instead of MOZ_MUST_USE

Now that Firefox is compiled as C++17 (bug 1560664), you can use C++17's 
[[nodiscard]] attribute [1] instead of the MOZ_MUST_USE macro (defined 
using clang and gcc's non-standard __attribute__((warn_unused_result))).

I have been slowly replacing MOZ_MUST_USE with [[nodiscard]] in my free 
time and hope to eventually remove the MOZ_MUST_USE definition itself. 
That is meta bug 1571631.

In the meantime, please:

1. Avoid adding more uses of MOZ_MUST_USE. Use [[nodiscard]].

2. Consider making more functions use [[nodiscard]] when writing or 
reviewing new code. Functions that return errors as nsresult or bool are 
probably good candidates for [[nodiscard]].

(I looked at adding [[nodiscard]] to the nsresult type definition, but 
the results were too noisy.)

One caveat: the [[nodiscard]] attribute must precede all of a function 
declaration's declaration specifiers (like static, extern, inline, or 
virtual). The __attribute__((warn_unused_result)) attribute (and thus 
MOZ_MUST_USE) does not have this order restriction.

- static inline MOZ_MUST_USE nsresult SomeFunction();
+ [[nodiscard]] static inline nsresult SomeFunction();

Once __attribute__((warn_unused_result)) has been replaced with 
[[nodiscard]], we can also remove mozilla::Unused, replacing `Unused <<` 
with a more idiomatic `(void)` cast (bug 1628542).

[[nodiscard]] can also be applied to types, so we may be able to replace 
our custom MOZ_MUST_USE_TYPE clang plugin with [[nodiscard]].

[1] https://en.cppreference.com/w/cpp/language/attributes/nodiscard
0
Chris
12/22/2020 12:20:33 AM
mozilla.dev.platform 6651 articles. 1 followers. Post Follow

1 Replies
233 Views

Similar Articles

[PageSpeed] 5
Get it on Google Play
Get it on Apple App Store

Hello Chris,

Thank you so much for doing this. =46rom a static-analysis perspective this h=
elps us greatly avoid possible false positives and enforcing the rule when a=
n actual result return from a function call shouldn=E2=80=99t be discarded.

Andi

> On 22 Dec 2020, at 02:25, Chris Peterson <cpeterson@mozilla.com> wrote:
>=20
> =EF=BB=BFNow that Firefox is compiled as C++17 (bug 1560664), you can use C=
++17's [[nodiscard]] attribute [1] instead of the MOZ_MUST_USE macro (define=
d using clang and gcc's non-standard __attribute__((warn_unused_result))).
>=20
> I have been slowly replacing MOZ_MUST_USE with [[nodiscard]] in my free ti=
me and hope to eventually remove the MOZ_MUST_USE definition itself. That is=
 meta bug 1571631.
>=20
> In the meantime, please:
>=20
> 1. Avoid adding more uses of MOZ_MUST_USE. Use [[nodiscard]].
>=20
> 2. Consider making more functions use [[nodiscard]] when writing or review=
ing new code. Functions that return errors as nsresult or bool are probably g=
ood candidates for [[nodiscard]].
>=20
> (I looked at adding [[nodiscard]] to the nsresult type definition, but the=
 results were too noisy.)
>=20
> One caveat: the [[nodiscard]] attribute must precede all of a function dec=
laration's declaration specifiers (like static, extern, inline, or virtual).=
 The __attribute__((warn_unused_result)) attribute (and thus MOZ_MUST_USE) d=
oes not have this order restriction.
>=20
> - static inline MOZ_MUST_USE nsresult SomeFunction();
> + [[nodiscard]] static inline nsresult SomeFunction();
>=20
> Once __attribute__((warn_unused_result)) has been replaced with [[nodiscar=
d]], we can also remove mozilla::Unused, replacing `Unused <<` with a more i=
diomatic `(void)` cast (bug 1628542).
>=20
> [[nodiscard]] can also be applied to types, so we may be able to replace o=
ur custom MOZ_MUST_USE_TYPE clang plugin with [[nodiscard]].
>=20
> [1] https://en.cppreference.com/w/cpp/language/attributes/nodiscard
> _______________________________________________
> dev-platform mailing list
> dev-platform@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-platform
0
Andi
12/22/2020 8:20:38 AM
Reply: