Here is a little program (live on godbolt):
static void UnusedDeclaration();
static void UnusedDefinition() {}
static void Declaration();
decltype(Declaration())* global;
Ideally I would expect the following warnings, if I compile it with clang, -Wunused:
UnusedDeclaration(): It is an unused function declaration with internal linkage. So I should get a warning.UnusedDefinition(): It is an unused function definition with internal linkage. So I should get a warning.Declaration(): This declaration is used. So I should not get a warning.
Actually all three cases get a warning:
warning: unused function 'UnusedDeclaration'
warning: unused function 'UnusedDefinition'
warning: function 'Declaration' is not needed and will not be emitted
I have a problem with case 3. I think, the compiler should not warn me about anything, but it does.
- Why do I get a warning for case 3?
function 'Declaration' is not needed– I honestly need that declaration. I use it in an unevaluated context, the compiler does not need to complain about it.and will not be emitted– I am not sure what emitting means. I have written a separate question about it.
- Is there a combination of warning flags to pass to the compiler on the command line, to achieve what I expect (warning for case 1 and 2, no warning for case 3).
- I could turn off warning for case 3 with
-Wunused -Wno-unneeded-internal-declaration, but I don't know if I lose some important information with it. Maybe in some other cases this warning is useful. In what exact situation does this 3rd warning come?