public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency
@ 2023-11-09 16:38 vincent-gcc at vinc17 dot net
  2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2023-11-09 16:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112463
           Summary: ternary operator / -Wsign-compare inconsistency
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

-Wsign-compare is described in the man page as follows:

  -Wsign-compare
    Warn when a comparison between signed and unsigned  values  could
    produce an incorrect result when the signed value is converted to
    unsigned.   In C++, this warning is also enabled by -Wall.  In C,
    it is also enabled by -Wextra.

But it can emit a warning even in the absence of comparisons between signed and
unsigned values. For instance, it can appear due to the 2nd and 3rd operands of
the ternary operator (these operands are not compared, just selected from the
value of the first operand). This affects the warning output by -Wextra.

Consider the following C code:

#include <stdio.h>
int main (void)
{
  for (int c = -1; c <= 1; c++)
    {
      long long
        i = c == 0 ? 0LL : (c >= 0 ? 1U : -1),
        j = c >= 0 ? (c == 0 ? 0LL : 1U) : -1;
      printf ("i = %lld\nj = %lld\n", i, j);
    }
  return 0;
}

(which shows that the ternary operator is not associative due to type
conversions). With -Wextra, I get:

ternary-op.c: In function ‘main’:
ternary-op.c:7:43: warning: operand of ‘?:’ changes signedness from ‘int’ to
‘unsigned int’ due to unsignedness of other operand [-Wsign-compare]
    7 |         i = c == 0 ? 0LL : (c >= 0 ? 1U : -1),
      |                                           ^~

But the "-Wsign-compare" is incorrect as there are no comparisons between
signed and unsigned values. Only -Wsign-conversion should trigger a warning.

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

end of thread, other threads:[~2023-11-10  9:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-09 16:38 [Bug c/112463] New: ternary operator / -Wsign-compare inconsistency vincent-gcc at vinc17 dot net
2023-11-10  3:01 ` [Bug c/112463] " egallager at gcc dot gnu.org
2023-11-10  3:22 ` pinskia at gcc dot gnu.org
2023-11-10  9:02 ` vincent-gcc at vinc17 dot net

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