public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator
@ 2022-11-21 17:41 redi at gcc dot gnu.org
  2022-11-21 17:59 ` [Bug c++/107797] " mpolacek at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-21 17:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

            Bug ID: 107797
           Summary: "warning right operand of comma operator has no
                    effect" for expressions with no comma operator
           Product: gcc
           Version: 13.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: ---

int main() {
    (long)new int{};
    long(new int{});
}

With -Wall this gives:

comma.cc: In function ‘int main()’:
comma.cc:2:20: warning: right operand of comma operator has no effect
[-Wunused-value]
    2 |     (long)new int{};
      |                    ^
comma.cc:3:20: warning: right operand of comma operator has no effect
[-Wunused-value]
    3 |     long(new int{});
      |                    ^


There is no comma operator in the code.

The bogus warning has been there since at least 6.1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
@ 2022-11-21 17:59 ` mpolacek at gcc dot gnu.org
  2022-11-21 18:03 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-11-21 17:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
                 CC|                            |mpolacek at gcc dot gnu.org
   Last reconfirmed|                            |2022-11-21

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
  2022-11-21 17:59 ` [Bug c++/107797] " mpolacek at gcc dot gnu.org
@ 2022-11-21 18:03 ` pinskia at gcc dot gnu.org
  2022-11-21 18:12 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-21 18:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |4.1.2

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
C++98 testcase which has been failing since at least 4.1.2:
int main() {
    (long)new int();
    long(new int());
}

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
  2022-11-21 17:59 ` [Bug c++/107797] " mpolacek at gcc dot gnu.org
  2022-11-21 18:03 ` pinskia at gcc dot gnu.org
@ 2022-11-21 18:12 ` mpolacek at gcc dot gnu.org
  2022-11-21 18:19 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-11-21 18:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
The artificial COMPOUND_EXPR here comes from build_new_1:

  if (init_expr)
    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
  if (cookie_expr)
    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);

I think a few years ago I experimented with TREE_NO_WARNING, but that didn't
work.  But now we have suppress_warning et al so I may give it another shot.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-11-21 18:12 ` mpolacek at gcc dot gnu.org
@ 2022-11-21 18:19 ` pinskia at gcc dot gnu.org
  2023-01-19 19:03 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-21 18:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(gdb) p debug_generic_expr(expr)
(long int) (int *) D.2758
...

1209            if (implicit != ICV_CAST && !warning_suppressed_p (expr /* What
warning? */))
1210              new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA,
complain);
1211            else
1212              new_op1 = convert_to_void (op1, ICV_CAST, complain);

I suspect there was a missing supression for the compound_expr creation when
creating it.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-11-21 18:19 ` pinskia at gcc dot gnu.org
@ 2023-01-19 19:03 ` mpolacek at gcc dot gnu.org
  2023-01-23 21:42 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-01-19 19:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
This seems to work:

diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
index 0cbfd8060cb..d1e34d7de1a 100644
--- a/gcc/cp/cvt.cc
+++ b/gcc/cp/cvt.cc
@@ -711,8 +711,11 @@ ocp_convert (tree type, tree expr, int convtype, int
flags,
    return error_mark_node;
       if (e == TREE_OPERAND (expr, 1))
    return expr;
-      return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
-            TREE_OPERAND (expr, 0), e);
+      e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
+             TREE_OPERAND (expr, 0), e);
+      if (warning_suppressed_p (expr, OPT_Wunused_value))
+   suppress_warning (e, OPT_Wunused_value);
+      return e;
     }

   complete_type (type);
diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index f816c474cef..52e96fbe590 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3800,6 +3800,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type,
tree nelts,
   if (cookie_expr)
     rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);

+  suppress_warning (rval, OPT_Wunused_value);
+
   if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
     /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
        and return the call (which doesn't need to be adjusted).  */

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-01-19 19:03 ` mpolacek at gcc dot gnu.org
@ 2023-01-23 21:42 ` cvs-commit at gcc dot gnu.org
  2023-01-23 21:42 ` mpolacek at gcc dot gnu.org
  2023-01-23 21:43 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-01-23 21:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:e3585e6acdfd5c1793f877476647d2521620c95c

commit r13-5314-ge3585e6acdfd5c1793f877476647d2521620c95c
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jan 19 17:12:34 2023 -0500

    c++: Quash bogus -Wunused-value with new [PR107797]

    We shouldn't emit "right operand of comma operator has no effect"
    when that comma operator was created by the compiler for "new int{}".
    convert_to_void/COMPOUND_EXPR already checks warning_suppressed_p so
    we can just suppress -Wunused-value.

            PR c++/107797

    gcc/cp/ChangeLog:

            * cvt.cc (ocp_convert): copy_warning when creating a new
            COMPOUND_EXPR.
            * init.cc (build_new_1): Suppress -Wunused-value on
            compiler-generated COMPOUND_EXPRs.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wunused-value-1.C: New test.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-01-23 21:42 ` cvs-commit at gcc dot gnu.org
@ 2023-01-23 21:42 ` mpolacek at gcc dot gnu.org
  2023-01-23 21:43 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-01-23 21:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed for GCC 13.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/107797] "warning right operand of comma operator has no effect" for expressions with no comma operator
  2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2023-01-23 21:42 ` mpolacek at gcc dot gnu.org
@ 2023-01-23 21:43 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-23 21:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107797

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-01-23 21:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-21 17:41 [Bug c++/107797] New: "warning right operand of comma operator has no effect" for expressions with no comma operator redi at gcc dot gnu.org
2022-11-21 17:59 ` [Bug c++/107797] " mpolacek at gcc dot gnu.org
2022-11-21 18:03 ` pinskia at gcc dot gnu.org
2022-11-21 18:12 ` mpolacek at gcc dot gnu.org
2022-11-21 18:19 ` pinskia at gcc dot gnu.org
2023-01-19 19:03 ` mpolacek at gcc dot gnu.org
2023-01-23 21:42 ` cvs-commit at gcc dot gnu.org
2023-01-23 21:42 ` mpolacek at gcc dot gnu.org
2023-01-23 21:43 ` pinskia 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).