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

--- Comment #4 from Jonny Grant <jg at jguk dot org> ---
Hello

Implicit conversion can introduce bugs. I would like to detect implicit enum
conversions to other types in C and C++. How about just adding the C++ warnings
first to match clang in example below?

The following example does produce warnings in C++ with Clang trunk, but not C.
Likewise with MSVC, C++ warning, but no C warning.

G++ only warns on line (19), clang has the additional line (15) and line (17)
warnings.

Could G++ also add the additions warnings?




-O2 -Wall -Wextra -Wconversion -Werror

#include <stdio.h>

typedef enum
{
    a = -1
} a_t;

a_t f()
{
    return a;
}

int main()
{
    unsigned int b = f();

    a_t test = b;
    printf("%u %u", test, b);
    return b;
}


#1 with x86-64 clang (trunk)
<source>:15:22: error: implicit conversion changes signedness: 'a_t' to
'unsigned int' [-Werror,-Wsign-conversion]
    unsigned int b = f();
                 ~   ^~~
<source>:17:9: error: cannot initialize a variable of type 'a_t' with an lvalue
of type 'unsigned int'
    a_t test = b;
        ^      ~
<source>:19:12: error: implicit conversion changes signedness: 'unsigned int'
to 'int' [-Werror,-Wsign-conversion]
    return b;
    ~~~~~~ ^
3 errors generated.
Compiler returned: 1




#1 with x64 msvc v19.22
example.cpp
<source>(18): error C2440: 'initializing': cannot convert from 'unsigned int'
to 'a_t'
<source>(18): note: Conversion to enumeration type requires an explicit cast
(static_cast, C-style cast or function-style cast)
Compiler returned: 2



x86-64 gcc (trunk) - 484ms
#1 with x86-64 gcc (trunk)
<source>: In function 'int main()':
<source>:17:16: error: invalid conversion from 'unsigned int' to 'a_t'
[-fpermissive]
   17 |     a_t test = b;
      |                ^
      |                |
      |                unsigned int
Compiler returned: 1

Reply via email to