[Bug c/98819] Wall Wformat-signedness suggests %u for %u

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Jan 25 16:43:37 GMT 2021


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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-01-25
     Ever confirmed|0                           |1
          Component|c++                         |c
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
The comments in the code that chooses to use the 'u' conversion specifier in
the hint suggest this might indeed be due to a bug:

              if (conversion_char)
                /* We found a match, using the given conversion char - the
                   length modifier was incorrect (or absent).
                   Provide a suggestion using the conversion char with the
                   correct length modifier for the type.  */
                return xasprintf ("%s%c", len_modifier, conversion_char);

The "match" refers to the type for the specifier that matches the type of the
argument (i.e., 'i' for 2021).  No length modifier was provided but that's not
incorrect.  What is "incorrect" (but not unsafe) is the provided conversion
specifier.

But I'm surprised the warning triggers to begin with in this case, when the
signed argument is representable in the unsigned type without change. 
Ordinarily GCC avoids issuing warnings for code that's demonstrably safe (e.g.,
-Wchar-subscripts when the char subscript is in the non-negative range).

It seems to me the warning should not be issued in this case; when it is
issued, the hint should be corrected to match the type of the provided
argument.


More information about the Gcc-bugs mailing list