Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 298
Thomas Weller Avatar asked Nov 21 '25 15:11

Thomas Weller


2 Answers

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

like image 151
Adrian Mole Avatar answered Nov 24 '25 08:11

Adrian Mole


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

like image 26
phuclv Avatar answered Nov 24 '25 07:11

phuclv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!