Closed Bug 1571631 (nodiscard) Opened 2 years ago Closed 1 month ago

Replace MOZ_MUST_USE macro with C++17 attribute [[nodiscard]]

Categories

(Core :: MFBT, task, P3)

Firefox 88
task

Tracking

()

RESOLVED FIXED
88 Branch
Tracking Status
firefox-esr78 --- wontfix
firefox86 --- wontfix
firefox87 --- wontfix
firefox88 --- fixed

People

(Reporter: TYLin, Assigned: cpeterson)

References

(Blocks 3 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

Duplicate of this bug: 1593551

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://searchfox.org/mozilla-central/rev/35873cfc312a6285f54aa5e4ec2d4ab911157522/mfbt/Attributes.h#392

https://en.cppreference.com/w/cpp/language/attributes/nodiscard

TBD whether the MOZ_ALLOCATOR macro can also be switched to [[nodiscard]]:

https://searchfox.org/mozilla-central/rev/35873cfc312a6285f54aa5e4ec2d4ab911157522/mfbt/Attributes.h#355

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.

Alias: nodiscard
Priority: -- → P3

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();
Depends on: 1624776
Depends on: 1624786
Depends on: 1624789

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.

See Also: → 518881, 1624786
Depends on: 1625828
Depends on: 1625834
Depends on: 1625855

(In reply to Karl Tomlinson (:karlt) from comment #4)

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.

Can uses of Unused << then be globally replaced with (void)? Like:

- Unused << SendShowEvent(aData, aFromUser);
+ (void) SendShowEvent(aData, aFromUser);
Flags: needinfo?(karlt)

(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.

Flags: needinfo?(karlt)
Depends on: 1627490
Depends on: 1627496
Blocks: 1628542
Depends on: 1628961
Depends on: 1629315
Depends on: 1629316
Depends on: 1629317
Depends on: 1630511
Depends on: 1631684
Depends on: 1631685
Depends on: 1633286
Depends on: 1662629
Depends on: 1662961
Depends on: 1662964
Depends on: 1663058
Depends on: 1663217
Depends on: 1663237
Depends on: 1663297
Depends on: 1664099
Depends on: 1664100
Depends on: 1664374
Depends on: 1665270
Depends on: 1676797
Depends on: 1678969
Depends on: 1679361
Depends on: 1684089
Depends on: 1684091
Depends on: 1684092
Depends on: 1691889
Assignee: nobody → cpeterson
Severity: normal → N/A

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.

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

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

Keywords: leave-open
Pushed by cpeterson@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/362aed67c493
Replace MOZ_MUST_USE with [[nodiscard]] in layout/style/SharedStyleSheetCache.h. r=emilio
https://hg.mozilla.org/integration/autoland/rev/08fc8b587c05
Replace MOZ_MUST_USE with [[nodiscard]] in toolkit/components/commandlines/nsCommandLine.h. r=mossop
https://hg.mozilla.org/integration/autoland/rev/ad427a2f4a2f
Replace MOZ_MUST_USE with [[nodiscard]] in js/src. r=jandem
Pushed by cpeterson@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/32c92dfb8fe7
Replace MOZ_MUST_USE with [[nodiscard]] in mfbt. r=sg
https://hg.mozilla.org/integration/autoland/rev/f9a8f6b334ef
Remove MOZ_MUST_USE definition. r=sg
Status: NEW → ASSIGNED
Keywords: leave-open
Version: unspecified → Firefox 88
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 88 Branch
You need to log in before you can comment on or make changes to this bug.