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

--- Comment #9 from John Downing <jrdowning at yahoo dot com> ---
(In reply to Jason Merrill from comment #7)

> Rather, it was suppressed for the simple case where the RHS has a suitable
> type.  As Jonathan says, (CHAR_BIT * static_cast<short>(i)) has type int (it
> would be unsigned without the cast).  And then the << expression has type
> int.  And then the | expression has type int.
> 
> Before the patch for 40752, we would warn based just on the type of the |
> expression.  Now we also look at the type of the << expression, see that it
> is also int, and warn.  You can avoid the warning by breaking up the
> expression more:
> 
> #define CHAR_BIT 8
> void print_byte_order( short )
> {
>    short val = 0;
>    unsigned i = 1;
>    short pat1 = (CHAR_BIT * static_cast<short>(i));
>    short pat2 = pat1 << (CHAR_BIT * static_cast<short>(i));
>    val |= pat2;
> }
> 
> or adding another cast as in comment #1.

The code I posted has two casts, as in comment #1.  When I compile code like
this:

#define CHAR_BIT 8

void print_byte_order_short( short )
{
   short val = 0;
   unsigned int i =1;

   for(i = 1; i < sizeof(short); ++i)
   {
      short part1 =  (CHAR_BIT * static_cast<short>(i));
      short part2 = part1 << CHAR_BIT * static_cast<short>(i);
      val |= part2;
   }
   cout << val;
}

I get this warning:

example2.cpp: In function void print_byte_order_short(short int):
example2.cpp:15:32: warning: conversion from int to short int may change value
[-Wconversion]
       short part1 =  (CHAR_BIT * static_cast<short>(i));
                      ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
example2.cpp:16:27: warning: conversion from int to short int may change value
[-Wconversion]
       short part2 = part1 << CHAR_BIT * static_cast<short>(i);
                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Which seems like the same "design decision" I referenced in comment #8.

Reply via email to