Bug 106940 - Feature request: -Wsuggest-noexcept and -Wsuggest-explicit
Summary: Feature request: -Wsuggest-noexcept and -Wsuggest-explicit
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
Reported: 2022-09-14 08:56 UTC by felix
Modified: 2022-09-15 01:16 UTC (History)
1 user (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description felix 2022-09-14 08:56:14 UTC
When writing classes, I routinely tend to forget that constructors can be used for implicit conversions, unless marked explicit.

I also tend to forget about noexcept specifiers, which can lead to performance pitfalls, especially with move constructors and move assignment operators.

So I would like a compiler warning that could remind me to consider such concerns.  In case of noexcept, I don’t particularly need full semantic analysis of the body; a pure syntax check whether the specifier is present should suffice.  And in case of explicit, no other kind of check is possible, obviously.

I imagine -Wsuggest-explicit could be suppressed for copy and move constructors from the same type.  I am undecided whether it should apply to conversion operators, but leaning towards yes.

The warnings would be silenced by adding explicit specifiers: explicit(false) and noexcept(false).  Each warning should probably require the earliest language version where the respective silencing is possible (C++20 and C++11, respectively).