Places where assignment operator can be used wrongly instead of comparison operator , = Vs ==

Viewed 106

I'm doing a C++ compiler project where I need to flag warnings at places where assignment operator can be used wrongly instead of the comparison operator. .e.g while doing comparison in if statement , to check variable a as 10 sometimes we wrongly type if(a= 10), which will result in if statement always true whereas I wanted to be true only if a is 10. Some of the cases I can think of are :

  1. if(var = a), logically it should be if( var==a )
  2. while(var = a )
  3. for(;var=a;)
  4. do{}while(var=a)
  5. var=a? "some XYZ": "some ABC"

Can you please help me with the more cases where this logical error can occur, where the user was supposed to use == and by mistake = was used?

1 Answers

A nasty one I ran across recently was assert (a=b). The reason that's so particularly nasty is because the assumption stated in the assert is that the two are already equal, so the statement is most likely harmless. But if they aren't, this sets you up for a nasty debugging session as the debug builds functionally differs from the release build.

Related