3

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.

10
  • Most compilers will issue a warning, just turn warnings into errors using your specific compile flags Commented Oct 9, 2022 at 13:31
  • 1
    Isn't warning C4706: assignment within conditional expression suitable? Commented Oct 9, 2022 at 13:31
  • @πάνταῥεῖ: I heard people say so, yes. My question is to how actually do that in Visual Studio. Commented Oct 9, 2022 at 13:33
  • 1
    godbolt.org/z/MWvn3joEa Commented Oct 9, 2022 at 13:34
  • @ThomasWeller in VS 2022 you're configuring Debug Win32 but building Debug x64. In VS 2019 the build fails so it didn't come to the warning stage for that line. Why do you even build Win32 these days? Commented Oct 9, 2022 at 13:52

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

Note that this directive will generate an error whatever the current warning level is set to in the command-line/project options. Even with /W1, I still get the error in VS2022.
Thank you very much. I edited your answer to include a screenshot on how to make a solution wide setting. The main problem for me was my selection of configuration and platform.
7

Use /W4 /WX

/WX Treats all compiler warnings as errors. For a new project, it may be best to use /WX in all compilations; resolving all warnings ensures the fewest possible hard-to-find code defects.

https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170

Make sure you make that setting in the relevant configurations. In such a simple demo, it's probably easiest to choose "all configurations" and "all platforms".

Visual Studio property pages

7 Comments

Or for just C4706 in particular, #pragma warning( error : 4706 ) is an option.
I have done this already. See my screenshots in the question. As I said, I don't get a warning for that, so there's nothing to be converted into an error.
@ThomasWeller I don't see the switch /W4 in your question
@MatG: So W4 is more than Wall?
no, /Wall is higher, but it includes so many redundant warnings so /W4 is more commonly used
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.