Subject: Re: [PATCH 4/8] parse-options: mark unused "opt" parameter in callbacks

From: Junio C Hamano <[email-masked]>
Date: Thu, 31 Aug 2023 09:33:22 -0700

Jeff King <[email-masked]> writes:

> diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
> index f62f13f2b5..95b3717dd1 100644
> --- a/builtin/checkout-index.c
> +++ b/builtin/checkout-index.c
> @@ -190,7 +190,7 @@ static const char * const builtin_checkout_index_usage[] = {
>  	NULL
>  };
>  
> -static int option_parse_stage(const struct option *opt,
> +static int option_parse_stage(const struct option *opt UNUSED,
>  			      const char *arg, int unset)
>  {
>  	BUG_ON_OPT_NEG(unset);

I suspect that the original is buggy; when given

  $ git checkout-index --stage=all --stage=1 path

the first option turns --temp on, but the second one does not turn
it off.  For now I think being bug-to-bug compatible and annotating
the opt as UNUSED is good, but as a follow-up, we could make the
caller:

 (1) point &checkout_stage with opt->value;

 (2) make to_tempfile to tristate <unspecified, false, true> by
     initializing it to -1;

 (3) adjust to_tempfile that is still <unspecified> after
     parse_options() returns, according to the value in
     checkout_stage.

and then this can follow the "opt->value points at the variable that
is affected" pattern.

> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index 8f93529505..cd2afe33e5 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -168,7 +168,8 @@ static int git_fetch_config(const char *k, const char *v,
>  	return git_default_config(k, v, ctx, cb);
>  }
>  
> -static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
> +static int parse_refmap_arg(const struct option *opt UNUSED,
> +			    const char *arg, int unset)
>  {
>  	BUG_ON_OPT_NEG(unset);

Can't this just point opt->value at the global &refmap?  Obviously
not a huge deal, as we could have taken the "annotate as UNUSED"
approach for all the functions in [3/8].

> diff --git a/builtin/gc.c b/builtin/gc.c
> index 369bd43fb2..b842349d86 100644
> --- a/builtin/gc.c
> +++ b/builtin/gc.c
> @@ -1403,7 +1403,7 @@ static void initialize_task_config(int schedule)
>  	strbuf_release(&config_name);
>  }
>  
> -static int task_option_parse(const struct option *opt,
> +static int task_option_parse(const struct option *opt UNUSED,
>  			     const char *arg, int unset)
>  {
>  	int i, num_selected = 0;

The TASK__COUNT constant is very closely tied to the task[] array
and it is not worth passing the address of the array in opt->value.
The loss of clarity inside the callback funtion is a more serious
downside than the value on the caller side to document what array
is being modified.  So I agree this is a good change.

> diff --git a/builtin/update-index.c b/builtin/update-index.c
> index aee3cb8cbd..59acae3336 100644
> --- a/builtin/update-index.c
> +++ b/builtin/update-index.c
> @@ -856,7 +856,7 @@ static int chmod_callback(const struct option *opt,
>  	return 0;
>  }
>  
> -static int resolve_undo_clear_callback(const struct option *opt,
> +static int resolve_undo_clear_callback(const struct option *opt UNUSED,
>  				const char *arg, int unset)
>  {
>  	BUG_ON_OPT_NEG(unset);
> @@ -890,7 +890,7 @@ static int parse_new_style_cacheinfo(const char *arg,
>  }
>  
>  static enum parse_opt_result cacheinfo_callback(
> -	struct parse_opt_ctx_t *ctx, const struct option *opt,
> +	struct parse_opt_ctx_t *ctx, const struct option *opt UNUSED,
>  	const char *arg, int unset)
>  {
>  	struct object_id oid;

These two refuses to take "arg" and the callback mechanism is used
purely to trigger the side effect of the function, not as a part of
parsing something for its "value", so I agree that these UNUSED
annotations reflect the reality very well.

Thanks.