Search code examples
c++visual-studiocompiler-warnings

How to get a compiler error for assignments in an if statement?


Is it possible in Visual Studio to get a compiler error for an assignment in an if statement? How?

#include <iostream>
int main()
{
    int a = 2;
    if (a = 3)  // Want a warning here
        std::cout << "Avoid this!\n";
}

I know I can switch to Yoda conditions (if (3=a)), but I really don't want to.

I tried: setting the warning level to /Wall but I still don't get a warning that I could then treat as an error.

I am doing this in Visual Studio 2019 (16.11.19).:

Visual Studio 2019

The build output is

Rebuild started...
1>------ Rebuild All started: Project: Yoda2019, Configuration: Debug Win32 ------
1>Yoda2019.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): error C2220: the following warning is treated as an error
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(154,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(164,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(284,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(299,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(315,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(375,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>Done building project "Yoda2019.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

and Visual Studio 2022 (17.4.0)

Visual Studio 2022

The build output is

Rebuild started...
1>------ Rebuild All started: Project: YodaCheck, Configuration: Debug x64 ------
1>YodaCheck.cpp
1>YodaCheck.vcxproj -> B:\Projekte\C++\Dynamic Linking\YodaCheck\x64\Debug\YodaCheck.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Elapsed 00:01,379 ==========

so I see no warning which I could convert into an error.


Solution

  • You can turn any specific warning into an error, using the #pragma warning(error:nnnn) directive. In your case, the warning is:

    warning C4706: assignment within conditional expression

    So, adding the relevant #pragma directive to the code will generate a compiler error:

    #include <iostream>
    #pragma warning(error:4706)
    
    int main()
    {
        int a = 2;
        if (a = 3)  // Want a warning here
            std::cout << "Avoid this!\n";
    }
    

    This now gives:

    error C4706: assignment within conditional expression

    For a solution wide setting, change it here in the property pages:

    C4706 in property page