Make VS compiler catch signed/unsigned assignments?

后端 未结 3 620
臣服心动
臣服心动 2021-01-19 18:33

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

相关标签:
3条回答
  • 2021-01-19 18:48

    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.

    0 讨论(0)
  • 2021-01-19 19:01

    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.

    0 讨论(0)
  • 2021-01-19 19:03

    @quamrana:

    There must be something beyond the /Wall option to enable warning 4365:

    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
    

    I see that Andrew got it to work, but does anyone have an idea why it's not working here?

    The Visual Studio docs indicate that it should, but I can't even get the example program in the docs to give the C4365 warning (though it does give the related C4245 warning - but that occurs with just a /W4 option anyway).

    0 讨论(0)
提交回复
热议问题