For my compiler tests, I need to generate this warning "Statement has no effect" in my test code. How can I do this?
Using VS cl.exe compiler
For my compiler tests, I need to generate this warning "Statement has no effect" in my test code. How can I do this?
Using VS cl.exe compiler
so ross$ cat > noeff.c
void f(void) {
1;
}
so ross$ cc -Wall -c noeff.c
noeff.c: In function ‘f’:
noeff.c:2: warning: statement with no effect
so ross$
cc --version returns i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)int main()
{
5; // Statement has no effect
return 0;
}
Edit 1 Tried on VC++ 2010
#include <iostream>
#pragma warning(default:4555)
int main()
{
5;
getchar();
return 0;
}
Output :
warning C4555:main.cpp(6): expression has no effect; expected expression with side-effect
NOTE: It seems VC++ 2010 has no C4705 warning on their list. MSDN Compiler Warnings
void f();
int main()
{
f; // Statement has no effect
}
One more:
x == 0;
I found one of these in some code (written by others) recently - I fixed it to the intended 'x = 0;'.
GCC 4.2.1 on MacOS X 10.6.6.
cc -Wall -c x.c
x.c: In function ‘f’:
x.c:5: warning: statement with no effect
Code:
int f(int x)
{
x *= 3;
if (x % 2 == 0)
x == 0;
return x;
}
What you get with other compilers depends on the other compiler.
The following C code generates the following warnings with VS2008 at whatever the default warning level is for that compiler:
int main()
{
int a = 0;
1; // this doesn't seem to generate a warning
a + 1;
a == 0;
return 0;
}
C:\temp\test.c(5) : warning C4552: '+' : operator has no effect; expected operator with side-effect
C:\temp\test.c(6) : warning C4553: '==' : operator has no effect; did you intend '='?
In your comments you seem to actually be focused on getting warning C4705 ("statement has no effect"). According to MSDN, it appears that that warning is only documented for VS6. So I think that if you want that specific error code, you'll need to dig up VC++ 6.