public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114104] New: nodiscard not diagnosed on synthesized operator!=
@ 2024-02-25 19:31 redi at gcc dot gnu.org
2024-02-25 19:35 ` [Bug c++/114104] " redi at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-25 19:31 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114104
Bug ID: 114104
Summary: nodiscard not diagnosed on synthesized operator!=
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
We fail to diagnose this C++20 code:
[[nodiscard]] bool operator==(X, int) { return true; }
int main() {
X x, y;
x != 0;
}
For x == 0 and 0 == x the nodiscard attribute works as expected, but not for x
!= 0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/114104] nodiscard not diagnosed on synthesized operator!=
2024-02-25 19:31 [Bug c++/114104] New: nodiscard not diagnosed on synthesized operator!= redi at gcc dot gnu.org
@ 2024-02-25 19:35 ` redi at gcc dot gnu.org
2024-02-25 20:58 ` harald at gigawatt dot nl
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-25 19:35 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114104
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Ever confirmed|0 |1
Last reconfirmed| |2024-02-25
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This bug means we fail to diagnose this example (courtesy of Stephan T.
Lavavej):
#include <print>
#include <vector>
using namespace std;
int main() {
vector<int> x{10, 20, 30};
vector<int> y{11, 22, 33, 44};
for (auto i = x.begin(), j = y.begin(); i != x.end(), j != y.end(); ++i,
++j) {
println("{} {}", *i, *j);
}
}
The loop condition should use && between the two expressions but it uses a
comma instead. Even though operator== for vector::iterator is marked nodiscard,
we fail to warn that i != x.end() is discarded.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/114104] nodiscard not diagnosed on synthesized operator!=
2024-02-25 19:31 [Bug c++/114104] New: nodiscard not diagnosed on synthesized operator!= redi at gcc dot gnu.org
2024-02-25 19:35 ` [Bug c++/114104] " redi at gcc dot gnu.org
@ 2024-02-25 20:58 ` harald at gigawatt dot nl
2024-02-25 23:34 ` redi at gcc dot gnu.org
2024-02-26 14:36 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: harald at gigawatt dot nl @ 2024-02-25 20:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114104
Harald van Dijk <harald at gigawatt dot nl> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |harald at gigawatt dot nl
--- Comment #2 from Harald van Dijk <harald at gigawatt dot nl> ---
Isn't this behaving as designed as far as nodiscard goes? x != 0 is defined to
be evaluated as !(x == 0) per [over.match.oper]p9, where the result of x == 0
is not a discarded-value expression, and therefore nodiscard suggests no
warning for it.
That said, there is a missing general warning in GCC about the built-in !
operator being discarded:
bool f();
int main() {
!f(); // clang: warning: expression result unused [-Wunused-value]
// gcc: no warning
}
For similar useless operations, such as f() ^ true;, GCC emits a similar
warning "warning: value computed is not used [-Wunused-value]". Presumably, if
that warning were implemented in GCC for ! as well, it should also fire for
your original x != 0 test?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/114104] nodiscard not diagnosed on synthesized operator!=
2024-02-25 19:31 [Bug c++/114104] New: nodiscard not diagnosed on synthesized operator!= redi at gcc dot gnu.org
2024-02-25 19:35 ` [Bug c++/114104] " redi at gcc dot gnu.org
2024-02-25 20:58 ` harald at gigawatt dot nl
@ 2024-02-25 23:34 ` redi at gcc dot gnu.org
2024-02-26 14:36 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-25 23:34 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114104
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I don't care whether GCC synthesizes an operator!= that inherits the nodiscard
attribute, or it generates a discarded bool using the built-in !, but there
needs to be a warning either way.
Currently a handwritten operator!= with the attribute gives better results than
letting the compiler do it, and that should not be the case.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/114104] nodiscard not diagnosed on synthesized operator!=
2024-02-25 19:31 [Bug c++/114104] New: nodiscard not diagnosed on synthesized operator!= redi at gcc dot gnu.org
` (2 preceding siblings ...)
2024-02-25 23:34 ` redi at gcc dot gnu.org
@ 2024-02-26 14:36 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-02-26 14:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114104
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ppalka at gcc dot gnu.org
--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to Harald van Dijk from comment #2)
> For similar useless operations, such as f() ^ true;, GCC emits a similar
> warning "warning: value computed is not used [-Wunused-value]". Presumably,
> if that warning were implemented in GCC for ! as well, it should also fire
> for your original x != 0 test?
That sounds plausible. The relevant code is
gcc/cp/cvt.cc
@@ -1647,11 +1647,6 @@ convert_to_void (tree expr, impl_conv_void implicit,
tsubst_flags_t complain)
enum tree_code code = TREE_CODE (e);
enum tree_code_class tclass = TREE_CODE_CLASS (code);
if (tclass == tcc_comparison
|| tclass == tcc_unary
|| tclass == tcc_binary
|| code == VEC_PERM_EXPR
|| code == VEC_COND_EXPR)
warn_if_unused_value (e, loc);
which doesn't consider boolean operations (TRUTH_NOT_EXPR / TRUTH_AND_EXPR /
TRUTH_OR_EXPR) because their class is tcc_expression. This is probably just an
oversight (even the C front end warns for !f()).
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-02-26 14:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-25 19:31 [Bug c++/114104] New: nodiscard not diagnosed on synthesized operator!= redi at gcc dot gnu.org
2024-02-25 19:35 ` [Bug c++/114104] " redi at gcc dot gnu.org
2024-02-25 20:58 ` harald at gigawatt dot nl
2024-02-25 23:34 ` redi at gcc dot gnu.org
2024-02-26 14:36 ` ppalka at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).