(Note I'm not entirely sure this is a GCC bug, but it doesn't happen with GCC < 11 and with clang) Reduced testcase: ``` #include <utility> extern void foo(...); template <typename... Ts> void bar(Ts&&... aArgs) { foo(std::forward<Ts>(aArgs)...); } void qux() { bar(nullptr); } ``` This emits the following warning: ``` <source>: In instantiation of 'void bar(Ts&& ...) [with Ts = {std::nullptr_t}]': <source>:11:8: required from here <source>:7:25: warning: ignoring return value of 'constexpr _Tp&& std::forward(typename std::remove_reference<_Tp>::type&) [with _Tp = std::nullptr_t; typename std::remove_reference<_Tp>::type = std::nullptr_t]', declared with attribute 'nodiscard' [-Wunused-result] 7 | foo(std::forward<Ts>(aArgs)...); | ~~~~~~~~~~~~~~~~^~~~~~~ In file included from /opt/compiler-explorer/gcc-11.1.0/include/c++/11.1.0/bits/stl_pair.h:59, from /opt/compiler-explorer/gcc-11.1.0/include/c++/11.1.0/utility:70, from <source>:1: /opt/compiler-explorer/gcc-11.1.0/include/c++/11.1.0/bits/move.h:77:5: note: declared here 77 | forward(typename std::remove_reference<_Tp>::type& __t) noexcept | ^~~~~~~ ``` (https://godbolt.org/z/GW3xf71n3) Incidentally, using the x86-64 gcc (static analysis) on compiler explorer fails with: ``` during IPA pass: analyzer <source>: In function 'void qux()': <source>:11:9: internal compiler error: in make_region_for_type, at analyzer/region-model.cc:5973 11 | bar(nullptr); | ^~~~~~~ Please submit a full bug report, with preprocessed source if appropriate. See <https://gcc.gnu.org/bugs/> for instructions. ```
(In reply to Mike Hommey from comment #0) > (Note I'm not entirely sure this is a GCC bug, but it doesn't happen with > GCC < 11 and with clang) Yeah, this shouldn't warn. Reduced: [[nodiscard]] decltype(nullptr) null(); extern void foo(...); void qux() { foo(null()); } GCC 10 has the same warning, it's just that std::forward didn't have the [[nodiscard]] attribute. It only happens when a nodiscard function returns std::nullptr_t and it's passed to an ellipsis. GCC 7 was OK, and 8.3 and 9.2 It started with r279680: re PR c++/92992 (Side-effects dropped when decltype(nullptr) typed expression is passed to ellipsis) PR c++/92992 * call.c (convert_arg_to_ellipsis): For decltype(nullptr) arguments that have side-effects use cp_build_compound_expr. * g++.dg/cpp0x/nullptr45.C: New test.
Created attachment 50862 [details] gcc12-pr100666.patch Untested fix.
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>: https://gcc.gnu.org/g:ad52d89808a947264397e920d7483090d4108f7b commit r12-1043-gad52d89808a947264397e920d7483090d4108f7b Author: Jakub Jelinek <jakub@redhat.com> Date: Tue May 25 17:24:38 2021 +0200 c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] When passing expressions with decltype(nullptr) type with side-effects to ellipsis, we pass (void *)0 instead, but for the side-effects evaluate them on the lhs of a COMPOUND_EXPR. Unfortunately that means we warn about it if the expression is a call to nodiscard marked function, even when the result is really used, just needs to be transformed. Fixed by adding a warning_sentinel. 2021-05-25 Jakub Jelinek <jakub@redhat.com> PR c++/100666 * call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE and side-effects, temporarily disable -Wunused-result warning when building COMPOUND_EXPR. * g++.dg/cpp1z/nodiscard8.C: New test. * g++.dg/cpp1z/nodiscard9.C: New test.
The releases/gcc-11 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>: https://gcc.gnu.org/g:718a78fcfb03ca3c552800b510ef3eb085bcbb76 commit r11-8492-g718a78fcfb03ca3c552800b510ef3eb085bcbb76 Author: Jakub Jelinek <jakub@redhat.com> Date: Tue May 25 17:24:38 2021 +0200 c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] When passing expressions with decltype(nullptr) type with side-effects to ellipsis, we pass (void *)0 instead, but for the side-effects evaluate them on the lhs of a COMPOUND_EXPR. Unfortunately that means we warn about it if the expression is a call to nodiscard marked function, even when the result is really used, just needs to be transformed. Fixed by adding a warning_sentinel. 2021-05-25 Jakub Jelinek <jakub@redhat.com> PR c++/100666 * call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE and side-effects, temporarily disable -Wunused-result warning when building COMPOUND_EXPR. * g++.dg/cpp1z/nodiscard8.C: New test. * g++.dg/cpp1z/nodiscard9.C: New test. (cherry picked from commit ad52d89808a947264397e920d7483090d4108f7b)
Fixed on the trunk and for 11.2 so far.
The releases/gcc-10 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>: https://gcc.gnu.org/g:589a0f34b1d176b62b064c9815c341b6abf48a2b commit r10-10614-g589a0f34b1d176b62b064c9815c341b6abf48a2b Author: Jakub Jelinek <jakub@redhat.com> Date: Tue May 25 17:24:38 2021 +0200 c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] When passing expressions with decltype(nullptr) type with side-effects to ellipsis, we pass (void *)0 instead, but for the side-effects evaluate them on the lhs of a COMPOUND_EXPR. Unfortunately that means we warn about it if the expression is a call to nodiscard marked function, even when the result is really used, just needs to be transformed. Fixed by adding a warning_sentinel. 2021-05-25 Jakub Jelinek <jakub@redhat.com> PR c++/100666 * call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE and side-effects, temporarily disable -Wunused-result warning when building COMPOUND_EXPR. * g++.dg/cpp1z/nodiscard8.C: New test. * g++.dg/cpp1z/nodiscard9.C: New test. (cherry picked from commit ad52d89808a947264397e920d7483090d4108f7b)
The releases/gcc-9 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>: https://gcc.gnu.org/g:6a05023c8739f2848778074e1ea9311cf6825a0f commit r9-10073-g6a05023c8739f2848778074e1ea9311cf6825a0f Author: Jakub Jelinek <jakub@redhat.com> Date: Tue May 25 17:24:38 2021 +0200 c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] When passing expressions with decltype(nullptr) type with side-effects to ellipsis, we pass (void *)0 instead, but for the side-effects evaluate them on the lhs of a COMPOUND_EXPR. Unfortunately that means we warn about it if the expression is a call to nodiscard marked function, even when the result is really used, just needs to be transformed. Fixed by adding a warning_sentinel. 2021-05-25 Jakub Jelinek <jakub@redhat.com> PR c++/100666 * call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE and side-effects, temporarily disable -Wunused-result warning when building COMPOUND_EXPR. * g++.dg/cpp1z/nodiscard8.C: New test. * g++.dg/cpp1z/nodiscard9.C: New test. (cherry picked from commit ad52d89808a947264397e920d7483090d4108f7b)
Fixed.