Make VS compiler compile signed / unsigned assignments?

The Visual Studio compiler does not seem to warn about signed / unsigned assignments, only when comparing. For example, the code below will generate a warning in an if statement, but not for initial purposes.

Is there anyway to get them to catch them? I'm already in W4, but I thought (hopes) that somewhere there might be a different setting.

Thank,

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 is disabled by default, even on W4. However, you can explicitly enable it for a given warning level, for example:

#pragma warning (4 : 4365)

The result is:

warning C4365: 'initializing' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
+3
source share
3 answers

You need to turn on warning 4365 to catch the target.

: - /Wall, , , .

+6

, /W [] []. , /W 34365 4365 3. , @[file] .

+2

@quamrana:

4365 - /Wall:

C:\Temp>cl /Wall /c foo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

foo.c
foo.c(6) : warning C4018: '<' : signed/unsigned mismatch

, , - , ?

Visual Studio docs indicate what it should be, but I can't even get the sample program in the docs to give warning C4365 (although it gives the corresponding warning C4245, but this only happens with the / W4 option).

0
source

Source: https://habr.com/ru/post/1696984/


All Articles