git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 31, 2023, 16:33 UTC
Message-ID
<xmqqil8vxjcd.fsf@gitster.g>
In-Reply-To
<20230831071820.GB3197751@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 13 quoted lines
> 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.

Show 13 quoted lines
> 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].

Show 13 quoted lines
> 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.

Show 22 quoted lines
> 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.
Previous: Jeff KingNext: Jeff King
Message 12 of 47 in “more unused parameters in parseopt callbacks”
  1. 0/8 more unused parameters in parseopt callbacksJeff King, Aug 31, 2023
  2. 1/8 merge: make xopts a strvecJeff King, Aug 31, 2023
  3. Jeff KingAug 31, 2023
  4. Phillip WoodAug 31, 2023
  5. Junio C HamanoAug 31, 2023
  6. Taylor BlauAug 31, 2023
  7. 2/8 merge: simplify parsing of "-n" optionJeff King, Aug 31, 2023
  8. Junio C HamanoAug 31, 2023
  9. 3/8 parse-options: prefer opt->value to globals in callbacksJeff King, Aug 31, 2023
  10. Junio C HamanoAug 31, 2023
  11. 4/8 parse-options: mark unused "opt" parameter in callbacksJeff King, Aug 31, 2023
  12. Junio C HamanoAug 31, 2023
  13. Jeff KingAug 31, 2023
  14. Jeff KingAug 31, 2023
  15. 5/8 merge: do not pass unused opt->value parameterJeff King, Aug 31, 2023
  16. Junio C HamanoAug 31, 2023
  17. 6/8 parse-options: add more BUG_ON() annotationsJeff King, Aug 31, 2023
  18. Junio C HamanoAug 31, 2023
  19. 7/8 interpret-trailers: mark unused "unset" parameters in option callbacksJeff King, Aug 31, 2023
  20. Junio C HamanoAug 31, 2023
  21. Jeff KingAug 31, 2023
  22. 8/8 parse-options: mark unused parameters in noop callbackJeff King, Aug 31, 2023
  23. Junio C HamanoAug 31, 2023
  24. 0/10 more unused parameters in parseopt callbacksJeff King, Aug 31, 2023
  25. 01/10 merge: make xopts a strvecJeff King, Aug 31, 2023
  26. 02/10 merge: simplify parsing of "-n" optionJeff King, Aug 31, 2023
  27. René ScharfeSep 2, 2023
  28. Jeff KingSep 5, 2023
  29. 03/10 format-patch: use OPT_STRING_LIST for to/cc optionsJeff King, Aug 31, 2023
  30. 04/10 checkout-index: delay automatic setting of to_tempfileJeff King, Aug 31, 2023
  31. Junio C HamanoAug 31, 2023
  32. René ScharfeSep 2, 2023
  33. 04/10 checkout-index: delay automatic setting of to_tempfileJeff King, Sep 5, 2023
  34. 05/10 parse-options: prefer opt->value to globals in callbacksJeff King, Aug 31, 2023
  35. René ScharfeSep 2, 2023
  36. Jeff KingSep 5, 2023
  37. 06/10 parse-options: mark unused "opt" parameter in callbacksJeff King, Aug 31, 2023
  38. René ScharfeSep 2, 2023
  39. Jeff KingSep 5, 2023
  40. René ScharfeSep 19, 2023
  41. 07/10 merge: do not pass unused opt->value parameterJeff King, Aug 31, 2023
  42. 08/10 parse-options: add more BUG_ON() annotationsJeff King, Aug 31, 2023
  43. 09/10 interpret-trailers: mark unused "unset" parameters in option callbacksJeff King, Aug 31, 2023
  44. 10/10 parse-options: mark unused parameters in noop callbackJeff King, Aug 31, 2023
  45. René ScharfeSep 2, 2023
  46. Jeff KingSep 5, 2023
  47. René ScharfeSep 7, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.