path: root/builtin/cat-file.c
diff options
authorJeff King <>2018-11-05 06:45:42 (GMT)
committerJunio C Hamano <>2018-11-06 03:56:29 (GMT)
commit517fe807d6903c629a739b23fe0e75b892096998 (patch)
tree532587a81f933fbe5d9b362f725a58b8bc4340a2 /builtin/cat-file.c
parent0a8a16ade6b3a55114cd0f28e5e71c2a3483d825 (diff)
assert NOARG/NONEG behavior of parse-options callbacks
When we define a parse-options callback, the flags we put in the option struct must match what the callback expects. For example, a callback which does not handle the "unset" parameter should only be used with PARSE_OPT_NONEG. But since the callback and the option struct are not defined next to each other, it's easy to get this wrong (as earlier patches in this series show). Fortunately, the compiler can help us here: compiling with -Wunused-parameters can show us which callbacks ignore their "unset" parameters (and likewise, ones that ignore "arg" expect to be triggered with PARSE_OPT_NOARG). But after we've inspected a callback and determined that all of its callers use the right flags, what do we do next? We'd like to silence the compiler warning, but do so in a way that will catch any wrong calls in the future. We can do that by actually checking those variables and asserting that they match our expectations. Because this is such a common pattern, we'll introduce some helper macros. The resulting messages aren't as descriptive as we could make them, but the file/line information from BUG() is enough to identify the problem (and anyway, the point is that these should never be seen). Each of the annotated callbacks in this patch triggers -Wunused-parameters, and was manually inspected to make sure all callers use the correct options (so none of these BUGs should be triggerable). Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/cat-file.c')
1 files changed, 2 insertions, 0 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 0f6b692..1cdd357 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -595,6 +595,8 @@ static int batch_option_callback(const struct option *opt,
struct batch_options *bo = opt->value;
+ BUG_ON_OPT_NEG(unset);
if (bo->enabled) {
return error(_("only one batch option may be specified"));