How to generate compiler warning "instruction has no effect" in c / C ++ code

For my compiler tests, I need to generate this “Statement has no effect” warning in my test code. How can i do this?

Using the VS cl.exe compiler

+3
source share
5 answers
int main()
{
    5;   // Statement has no effect
    return 0;
}

Edit 1 Done 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 that VC ++ 2010 does not have the C4705 warning on its list. MSDN Compiler Warnings

+2
source
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$ 
+6
source
void f();
int main()
{
   f; // Statement has no effect
}

http://ideone.com/oB9kf

+2

:

x == 0;

( ) - "x = 0;".

GCC 4.2.1 MacOS X 10.6.6.

cc -Wall -c x.c
x.c: In functionf’:
x.c:5: warning: statement with no effect

:

int f(int x)
{
    x *= 3;
    if (x % 2 == 0)
        x == 0;
    return x;
}

, , .

+1

C VS2008 :

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 '='?

, , C4705 ( " " ). MSDN, , VS6. , , V++ 6.

0

Source: https://habr.com/ru/post/1788649/


All Articles