Using the -Wunused-parameter flag, you can enforce __unused for unused parameters, as a compiler optimization. The following code causes two warnings:
#include <stdio.h>
int main(int argc, char **argv) {
printf("hello world\n");
return 0;
}
These warnings are fixed by adding __unused the unused arguments.
#include <stdio.h>
int main(int __unused argc, char __unused **argv) {
printf("hello world\n");
return 0;
}
When you use a parameter marked __unused, clang 4.1 does not warn or error.
#include <stdio.h>
int main(int __unused argc, char __unused **argv) {
printf("hello world. there are %d args\n", argc);
return 0;
}
The same behavior is exhibited using __attribute__((unused))
.
int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv) {
Is there a way to warn or error on __unused? What would happen should you accidentally leave an __unused on a used parameter? In the above example argc
appears to have the correct value, although it could be the compiler not taking advantage of the hint, and I wouldn't rely on this behavior without more understanding.