Unused Value Warning: C vs C ++ using gcc

Is it possible to make gcc descriptors of unused values ​​for C, as g ++ handles them for C ++? There p && ++*p;is a warning for type instructions in C, but not for C ++. Setting -wno-unused-valueup is not really a solution, as it will also remove useful warnings (as in line 2 below). Example:

$ (gcc --version|head -1; ls -l qwe.*; cat qwe.x; echo $sep; gcc -c -O2 -Wall qwe.c; echo $sep; g++ -c -O2 -Wall qwe.cc) 2>&1 | sed 's/^/ /'

gcc (Debian 4.9.2-10) 4.9.2
lrwxrwxrwx 1 tml tml    5 Jul 23 10:12 qwe.c -> qwe.x
lrwxrwxrwx 1 tml tml    5 Jul 23 10:12 qwe.cc -> qwe.x
-rw-r--r-- 1 tml tml 1384 Jul 23 10:49 qwe.o
-rw-r--r-- 1 tml tml   55 Jul 23 10:49 qwe.x
void g(int *p) { p && ++*p; }
void f(int *p) { *p+5; }
-------------------------------------------
qwe.c: In function β€˜g’:
qwe.c:1:20: warning: value computed is not used [-Wunused-value]
 void g(int *p) { p && ++*p; }
                    ^
qwe.c: In function β€˜f’:
qwe.c:2:18: warning: statement with no effect [-Wunused-value]
 void f(int *p) { *p+5; }
                  ^
-------------------------------------------
qwe.cc: In function β€˜void f(int*)’:
qwe.cc:2:20: warning: statement has no effect [-Wunused-value]
 void f(int *p) { *p+5; }
                    ^
+4
source share
1 answer

You can manually delete them by dropping them on void.

void f(int *p) { (void)*p+5; }

But there is reason for these warnings. In fact, they are not doing anything. There are no side effects. Thus, the result of calling this function is the same as not calling the function.

f(&x) .

+1

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


All Articles