public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Jeff King <peff@peff•net>
To: Deveshi Dwivedi <deveshigurgaon@gmail•com>
Cc: git@vger•kernel.org, gitster@pobox•com
Subject: coccinelle to catch pass-by-value?, was: [PATCH v1 1/2] worktree: do not pass strbuf by value
Date: Mon, 9 Mar 2026 15:26:00 -0400	[thread overview]
Message-ID: <20260309192600.GC309867@coredump.intra.peff.net> (raw)
In-Reply-To: <20260308180359.31188-2-deveshigurgaon@gmail.com>

On Sun, Mar 08, 2026 at 06:03:58PM +0000, Deveshi Dwivedi wrote:

> The function only needs the string values, not the strbuf machinery.
> Switch it to take const char * and update all callers to pass .buf.

Nice catch. I wonder if we can get the compiler or other static analysis
to complain about this mistake. The best I could come up with is:

diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci
index 5f06105df6..665f56d070 100644
--- a/contrib/coccinelle/strbuf.cocci
+++ b/contrib/coccinelle/strbuf.cocci
@@ -60,3 +60,10 @@ expression E1, E2;
 @@
 - strbuf_addstr(E1, real_path(E2));
 + strbuf_add_real_path(E1, E2);
+
+@@
+expression F, ARG1, ARG2;
+struct strbuf SB;
+@@
+- F(ARG1, SB, ARG2)
++ F(ARG1, &SB, ARG2)

It rewrites a non-pointer argument into a pointer. That's not enough to
actually make the code work, but it would alert a developer that they
needed to follow-through on the rest of it. Or maybe it would just
confuse them without further hints.

I think there may be a way to get coccinelle to just emit an error
message describing the situation, but it relies on python extensions,
which I'm not sure we currently require.

Anyway, your patch is obviously good and anything further we do would
want to come on top of it.

-Peff

  parent reply	other threads:[~2026-03-09 19:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-08 18:03 [PATCH v1 0/2] avoid unnecessary strbuf_split*() and strbuf-by-value usage Deveshi Dwivedi
2026-03-08 18:03 ` [PATCH v1 1/2] worktree: do not pass strbuf by value Deveshi Dwivedi
2026-03-09 14:48   ` Junio C Hamano
2026-03-09 19:26   ` Jeff King [this message]
2026-03-08 18:03 ` [PATCH v1 2/2] list-objects-filter-options: avoid strbuf_split_str() Deveshi Dwivedi
2026-03-09 15:38   ` Junio C Hamano
2026-03-09 19:01     ` Jeff King
2026-03-09 19:08   ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260309192600.GC309867@coredump.intra.peff.net \
    --to=peff@peff$(echo .)net \
    --cc=deveshigurgaon@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=gitster@pobox$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox