On 2025-12-24 03:23, Bruno Haible wrote:
Do we need the compiler to verify that the parameter is really unused?
No, we don't. Even if the code is later refactored in such a way that
the parameter gets used, there is no damage. It would be like a comment
that no longer applies.
I would *love* to have the compiler warn about comments that no longer
apply! And I think this is the heart of the taste disagreement. If I see
code like this:
int treesize = calculate_size (tree);
where treesize is not used anywhere else, I like the compiler to warn me
about it. Sure, the code is correct as-is, and there is no efficiency
damage if the compiler is smart enough and calculate_size lacks side
effects; but that line of code obstructs the core goal of
understandability. In some sense the unnecessary code is worse than a
comment that no longer applies, because it is not so easy for a human to
see that it does not affect execution.
Likewise for unused parameters.
I see several things going on here. First, it's useful to glance at a
function F's head and see whether F's arguments are really needed, or
simply pacify an API. MAYBE_UNUSED doesn't tell me that; UNNAMED does.
Second, not everyone agrees about whether and when unused parameters
should be diagnosed and/or unnamed. Witness GCC's -Wunused-parameter
(which is not enabled by default unless you use something like -Wall
-Wextra), or clang-tidy's misc-unused-parameters (which by default warns
about unused parameters only if the function body is nonempty). Also
witness C++, where a core guideline is "Unused parameters should be
unnamed"
<https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#rf-unused>.
Third, GCC's -Wunused-parameter can find real glitches in code. Although
Emacs currently disables the option, when I tried it I found many
arguments that were formerly used in old Emacs versions but the code had
bitrotted. The patch to fix this, which I have not applied yet, is over
4000 lines long; it does not use MAYBE_UNUSED or UNNAMED, but simply
omits arguments.
That project of using -Wunused-parameter in Emacs unfortunately also
resulted in many false positives. Although I initially employed only
MAYBE_UNUSED to pacify GCC, the result too often left me scratching my
head because of the first thing mentioned above, as Emacs has a lot of
conditional compilation. This encouraged me to distinguish UNNAMED from
MAYBE_UNUSED.