strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an other

The best way to add one strbuf to an other is via: strbuf_addbuf(&sb, &sb2); This is a bit more idiomatic and efficient than: strbuf_addstr(&sb, sb2.buf); because the size of the second strbuf is known and thus it can spare a strlen() call, and much more so than: strbuf_addf(&sb, "%s", sb2.buf); because it can spare the whole vsnprintf() formatting magic. Add new semantic patches to 'contrib/coccinelle/strbuf.cocci' to catch these undesired patterns and to suggest strbuf_addbuf() instead. Luckily, our codebase is already clean from any such undesired patterns (but one of the in-flight topics just tried to sneak in such a strbuf_addf() call). Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jan 25, 2019 at 13:25 UTC 28c23cd4c3902449aff72cb9a4a703220be0d6ac
1 file changed +30
contrib/coccinelle/strbuf.cocci
+30
@@ -12,6 +12,36 @@ constant fmt !~ "%";
12 )
13 );
14
15 +@@
16 +expression E;
17 +struct strbuf SB;
18 +format F =~ "s";
19 +@@
20 +- strbuf_addf(E, "%@F@", SB.buf);
21 ++ strbuf_addbuf(E, &SB);
22 +
23 +@@
24 +expression E;
25 +struct strbuf *SBP;
26 +format F =~ "s";
27 +@@
28 +- strbuf_addf(E, "%@F@", SBP->buf);
29 ++ strbuf_addbuf(E, SBP);
30 +
31 +@@
32 +expression E;
33 +struct strbuf SB;
34 +@@
35 +- strbuf_addstr(E, SB.buf);
36 ++ strbuf_addbuf(E, &SB);
37 +
38 +@@
39 +expression E;
40 +struct strbuf *SBP;
41 +@@
42 +- strbuf_addstr(E, SBP->buf);
43 ++ strbuf_addbuf(E, SBP);
44 +
45 @@
46 expression E1, E2;
47 format F =~ "s";