tmighty
tmighty

Reputation: 11409

C++: Make VS2010 complain about "If" with "="

Is there any way in VS2010 C++ to have the IDE complain if I do something like

if (somevar = 2)
{
    ...

?

I try to pay attention to writing == instead of =, but not only using C++ but also VB and other languages, it just happens sometimes.

It takes really much time sometimes to find errors associated with my fault.

I think the possiblity that one really wants to write an if statement that also assigns a new value to a variable (as in my example above) is rather small. That is why I am hoping that VS2010 might have included an option that tells me about my possible flaw, but I haven't found one.

Is there any option like this?

Upvotes: 3

Views: 332

Answers (2)

user1610015
user1610015

Reputation: 6678

Right-click the project name in Solution Explorer, select Properties, go to Configuration Properties | C/C++ | General, and set the Warning Level to Level4. You won't get an error but a warning:

warning C4706: assignment within conditional expression

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258648

Yes there is:

warning C4706: assignment within conditional expression

Just make sure you turn on all (not quite all on MSVS) warnings. (project properties -> C/C++ -> General -> Warning Level)

The alternative is using YODA conditions (2 == somevar), but that's ugly. :)

Upvotes: 6

Related Questions