views:

154

answers:

5

I have a section of code that can be summarised as follows;

void MyFunc()
{
   int x;
'
'
   x;  
'
'
}

I would have thought that simply referencing a variable, without modifying it in anyway or using its value in anyway should generate a warning. In VS2003 it does neither, and it I need lint to pick it up.

I realise it doesn't effect execution, but since it is a piece of code that does nothing, and the programmer doubtless intended to do something, why is it not flagged?

Similarly would you expect x = x to be a warning?

Edit: Modified question, as this constitutes a good candidate for a warning, but is not an error. Replies suggest this is handled better with other compilers. Will try out VS2008 later and post result.

+1  A: 

Such code might occur in a template class for metaprogramming purposes. For example, it might be some kind of a check whether x is accessible from the current context. Yes, it doesn't affect the result of execution, but it does affect the result of compilation; this might be of aid to techniques like SFINAE.

It seems, that it's of no help to compilation either. Funciton bodies don't count for choosing the proper template for a function call. And to check accessibility within a scope of a class, you have to use using operator for dependent names; this using operator itself being an accessibility check.

So, the code x; really has no effect.

Pavel Shved
@Pavel Shved, I don't see your point. If `x` is not accessible from the current context in a template instantiation, this results in a compilation error isn't it? Which beats SFINAE.
Péter Török
+1 for the edit and conclusions.
Shane MacLaughlin
+1  A: 

You'd expect a warning unless you cast the expression to void, i.e.

void MyFunc()
{
   int x;

   (void)x;  

}

What warning level do you have set ?

Paul R
/W4, highest level in VS2003, yup I'd expect a warning. (As he spends a morning trwaling through lint output).
Shane MacLaughlin
`gcc -Wall` gives: `lint.c:5: warning: statement with no effect`
Paul R
Intel's ICC gives: `lint.c(5): remark #174: expression has no effect` and `lint.c(5): warning #592: variable "x" is used before its value is set`
Paul R
A: 

why would it give a error, the syntax is correct.

Grumpy
Perhaps not an error, but certainly a warning. if (x = y) is syntactically correct also, but generally not what the programmer intended. I'd say any code that is in all probability not what the programmer intended should generate a warning, and I can't see that adding a statement to the code that does nothing the intention of most programmers at most times.
Shane MacLaughlin
A: 

Both single-variable statements (such as x;) and self-assignment (such as x=x) are valid code in C++, so the compiler can't flag them as errors, but a good compiler is of course allowed to give a warning that they don't have any effect, and might be mistakes by the programmer. For example, the compiler g++ gives the warning "statement has no effect" for x;.

Thomas Padron-McCarthy
+1  A: 

You need to use a better compiler :-) Compiled with the -Wall and -pedantic flags, the GCC C++ compiler given this code:

int main() {
    int x = 0;
    x;
}

produces this diagnostic:

ma.cpp:3: warning: statement has no effect
anon
PC-Lint does the same for me, just very slowly ;)error 522: (Warning -- Expected void type, assignment, increment or decrement) Surprising how many of these I'm coming across linting a large code base, all errors in this case.
Shane MacLaughlin
@Shane If PC-Lint is too slow, you might want to investigate GCC. The MinGW port (get the version at http://tdragon.net/recentgcc) will happily compile Windows code.
anon