[Bug c++/52697] New: -Wsign-conversion warns for obviously correct code with implicit promotion to int

DeusExSophismata at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Mar 24 00:50:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52697

             Bug #: 52697
           Summary: -Wsign-conversion warns for obviously correct code
                    with implicit promotion to int
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: trivial
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: DeusExSophismata@gmail.com


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.6.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-linker-build-id
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin
--enable-java-awt=gtk --disable-dssi
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix
gcc version 4.6.2 20111027 (Red Hat 4.6.2-1) (GCC) 




// test.cpp
unsigned f (unsigned short a) {
    return (a) ? a : 0;
}
int main () {
    return 0;
}


// command line
$ g++ test.cpp -Wsign-conversion
test.cpp: In function ‘unsigned int f(int)’:
test.cpp:2:25: warning: conversion to ‘unsigned int’ from ‘int’ may change the
sign of the result [-Wsign-conversion]




What I find interesting is the wide array of code that does not trigger a
warning. None of the following definitions of f() cause a warning:

unsigned f (unsigned short a) {
    return (a) ? a : a;
}

unsigned f (unsigned short a) {
    return (a) ? a : 0u;
}

unsigned f (unsigned short a) {
    return (true) ? a : 0;
}
unsigned f (unsigned short a) {
    return (false) ? a : 0;
}


It seems that there is some sort of error in the implicit conversion to int for
certain types of arithmetic.

This may be related to PR 38470
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38470)



More information about the Gcc-bugs mailing list