Replace MOZ_MUST_USE macro with C++17 attribute [[nodiscard]]
Categories
(Core :: MFBT, task, P3)
Tracking
()
People
(Reporter: TYLin, Assigned: cpeterson)
References
(Blocks 2 open bugs)
Details
Attachments
(5 files)
After we build as C++17 by default, we can replace MOZ_MUST_USE
with C++17 [[nodiscard]]
.
https://en.cppreference.com/w/cpp/language/attributes/nodiscard
Assignee | ||
Updated•5 years ago
|
Assignee | ||
Comment 2•5 years ago
|
||
The MOZ_MUST_USE macro is defined as clang's and gcc's nonstandard __attribute__((warn_unused_result))
. After we compile as C++17 by default (bug 1560664), we can replace MOZ_MUST_USE with C++17's standard [[nodiscard]]
attribute.
https://en.cppreference.com/w/cpp/language/attributes/nodiscard
TBD whether the MOZ_ALLOCATOR macro can also be switched to [[nodiscard]]:
However, these things are never as straightforward as they seem. clang allows __attribute__((warn_unused_result))
in some places that [[nodiscard]]
is not. Perhaps those __attribute__((warn_unused_result))
uses can be reordered to allow [[nodiscard]]
or they should remain.
Assignee | ||
Comment 3•5 years ago
•
|
||
Unfortunately, this change will be more complicated than a global find and replace of MOZ_MUST_USE
for [[nodiscard]]
. The [[nodiscard]]
attribute must precede a function declaration's declaration specifiers (like static
, extern
, inline
, or virtual
). The __attribute__((warn_unused_result))
attribute did not have this order restriction, so we have hundreds of cases like:
extern MOZ_MUST_USE bool foo();
inline MOZ_MUST_USE bool foo();
static MOZ_MUST_USE bool foo();
static inline MOZ_MUST_USE bool foo();
virtual MOZ_MUST_USE bool foo();
that will need to be rewritten as:
[[nodisard]] extern bool foo();
[[nodisard]] inline bool foo();
[[nodisard]] static bool foo();
[[nodisard]] static inline bool foo();
[[nodisard]] virtual bool foo();
Comment 4•5 years ago
|
||
The behavior of nodiscard
differs from warn_unused_result
in gcc, where a void
cast does not suppress warn_unused_result
warnings. The difference does not exist in clang.
Moving to nodiscard
will make mozilla::Unused
obsolete.
Updated•5 years ago
|
Assignee | ||
Comment 5•5 years ago
|
||
(In reply to Karl Tomlinson (:karlt) from comment #4)
The behavior of
nodiscard
differs fromwarn_unused_result
in gcc, where avoid
cast does not suppresswarn_unused_result
warnings. The difference does not exist in clang.Moving to
nodiscard
will makemozilla::Unused
obsolete.
Can uses of Unused <<
then be globally replaced with (void)
? Like:
- Unused << SendShowEvent(aData, aFromUser);
+ (void) SendShowEvent(aData, aFromUser);
Comment 6•5 years ago
|
||
(In reply to Chris Peterson [:cpeterson] from comment #5)
Can uses of
Unused <<
then be globally replaced with(void)
? Like:- Unused << SendShowEvent(aData, aFromUser); + (void) SendShowEvent(aData, aFromUser);
Yes, except that clang-format prefers no space after (void)
.
I'm not a style peer, but at least one peer would appreciate that.
Assignee | ||
Updated•4 years ago
|
Assignee | ||
Comment 7•4 years ago
|
||
MOZ_MUST_USE was replaced with [[nodiscard]] in layout in bug 1625855, but some new uses of MOZ_MUST_USE instead of [[nodiscard]] have crept back in.
Assignee | ||
Comment 8•4 years ago
|
||
Depends on D108342
Assignee | ||
Comment 9•4 years ago
|
||
MOZ_MUST_USE was replaced with [[nodiscard]] in js/rc in bug 1684092, but some new uses of MOZ_MUST_USE instead of [[nodiscard]] have crept back in.
Depends on D108343
Assignee | ||
Comment 10•4 years ago
|
||
Also move MOZ_MUST_USE before function declarations' specifiers and return type. While clang and gcc's attribute((warn_unused_result)) can appear before, between, or after function specifiers and return types, the [[nodiscard]] attribute must precede the function specifiers.
Depends on D108344
Assignee | ||
Comment 11•4 years ago
|
||
Depends on D108345
Assignee | ||
Updated•4 years ago
|
Comment 12•4 years ago
|
||
Comment 13•4 years ago
|
||
bugherder |
Comment 14•4 years ago
|
||
Assignee | ||
Updated•4 years ago
|
Comment 15•4 years ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/32c92dfb8fe7
https://hg.mozilla.org/mozilla-central/rev/f9a8f6b334ef
Description
•