Hacker News new | past | comments | ask | show | jobs | submit login

The "unused parameter" error is one of the most useless of C compiler errors. There are dozens of perfectly valid reasons why a parameter would go unused, so the occasional error just gets lost in the noise.

It's more helpful in C++, which can omit parameter names to indicate that it's intentionally unused. But then you're using C++; not worth the tradeoff.




It's not hard to type a statement like "(void)v;" to explicitly say, "v is intentionally unused." I fail to see the drawback of this approach. Of course you should only do this when (1) you can't remove the parameter from the function's prototype and (2) there is a good reason for it to be unused. But once you develop that habit, the compiler can help you catch bugs that are otherwise easy to miss. Sounds like a worthwhile tradeoff to me.


What you want is __attribute__((unused)), assuming you're compiling with gcc.


Then you get a "statement with no effect" warning, which is usually more serious. I'll take a bogus warning in real code over a bogus warning in contrived code any day.


I've never seen a compiler issue a warning about "(void)foo;". Even compilers which issue 'statement with no effect' warnings normally recognize deliberate casts to void as a way of saying "shut up, I know what I'm doing".


>There are dozens of perfectly valid reasons why a parameter would go unused, so the occasional error just gets lost in the noise.

Such as?


I can only think of a few, but they're valid:

1. An API-defined callback signature where you need to use (for example) the first and third parameters, and not the second.

2. A public API function that has become deprecated and the previous behavior is emulated in a way that doesn't require all the parameters.

3. Stub/wrapper functions.

4. Init/cleanup functions for modules written to a plugin API, when all parameters passed are not needed in all cases.

5. Language binding closure/thunk functions.

Most of the uses, I think, boil down to backwards compat and fixed callback/plugin interfaces. In those cases I think an __attribute__((unused)) or void cast aren't a huge burden. I usually turn on -Wunused-parameter just in case.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: