Skip to content

Commit 28c23cd

Browse files
szedergitster
authored andcommitted
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>
1 parent 0d0ac38 commit 28c23cd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

contrib/coccinelle/strbuf.cocci

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ constant fmt !~ "%";
1212
)
1313
);
1414

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+
1545
@@
1646
expression E1, E2;
1747
format F =~ "s";

0 commit comments

Comments
 (0)