Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make VS compiler catch signed/unsigned assignments?

The Visual Studio compiler does not seem to warn on signed/unsigned assignments, only on comparisons. For example the code below will generate a warning on the if statement but not the initial assignments.

Is there anyway to make it catch these? I'm already at W4 but thought (hoped) there may be another setting somewhere.

Thanks,

int foo(void)
{
    unsigned int fooUnsigned = 0xffffffff;
    int fooSigned = fooUnsigned; // no warning

    if (fooSigned < fooUnsigned) // warning
    {
        return 0;
    }

    return fooSigned;
}

Update:

Quamrana is right, this is controlled by warning 4365 which appears to be off by default, even at W4. However you can explicitly enable it for a given warning level like so;

#pragma warning (4 : 4365)

Which results in;

warning C4365: 'initializing' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
like image 886
Andrew Grant Avatar asked Jan 24 '23 03:01

Andrew Grant


2 Answers

You need to enable warning 4365 to catch the assignment.

That might be tricky - you need to enable ALL warnings - use /Wall which enables lots of warnings, so you may have some trouble seeing the warning occur, but it does.

like image 94
quamrana Avatar answered Jan 26 '23 15:01

quamrana


You can change the level of any specific warning by using /W[level][code]. So in this case /W34365 will make warning 4365 into a level 3 warning. If you do this a lot you might find it useful to put these options in a text file and use the @[file] option to simplify the command line.

like image 22
ottibus Avatar answered Jan 26 '23 15:01

ottibus