{"thread":{"id":"50319","subject":"[PATCH] strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an other","startedAt":"2019-01-25T12:25:32Z","lastAt":"2019-01-25T12:25:32Z","messageCount":1,"participants":["SZEDER Gábor"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"367653","messageId":"20190125122517.29026-1-szeder.dev@gmail.com","threadId":"50319","inReplyTo":null,"subject":"[PATCH] strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an other","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2019-01-25T12:25:17Z","receivedAt":"2019-01-25T12:25:32Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The best way to add one strbuf to an other is via:\n\n  strbuf_addbuf(&sb, &sb2);\n\nThis is a bit more idiomatic and efficient than:\n\n  strbuf_addstr(&sb, sb2.buf);\n\nbecause the size of the second strbuf is known and thus it can spare a\nstrlen() call, and much more so than:\n\n  strbuf_addf(&sb, \"%s\", sb2.buf);\n\nbecause it can spare the whole vsnprintf() formatting magic.\n\nAdd new semantic patches to 'contrib/coccinelle/strbuf.cocci' to catch\nthese undesired patterns and to suggest strbuf_addbuf() instead.\n\nLuckily, our codebase is already clean from any such undesired\npatterns (but one of the in-flight topics just tried to sneak in such\na strbuf_addf() call).\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n\nInspired by:\n\n  https://public-inbox.org/git/20190125112203.GB6702@szeder.dev/\n\n contrib/coccinelle/strbuf.cocci | 30 ++++++++++++++++++++++++++++++\n 1 file changed, 30 insertions(+)\n\ndiff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci\nindex e34eada1ad..d9ada69b43 100644\n--- a/contrib/coccinelle/strbuf.cocci\n+++ b/contrib/coccinelle/strbuf.cocci\n@@ -12,6 +12,36 @@ constant fmt !~ \"%\";\n )\n   );\n \n+@@\n+expression E;\n+struct strbuf SB;\n+format F =~ \"s\";\n+@@\n+- strbuf_addf(E, \"%@F@\", SB.buf);\n++ strbuf_addbuf(E, &SB);\n+\n+@@\n+expression E;\n+struct strbuf *SBP;\n+format F =~ \"s\";\n+@@\n+- strbuf_addf(E, \"%@F@\", SBP->buf);\n++ strbuf_addbuf(E, SBP);\n+\n+@@\n+expression E;\n+struct strbuf SB;\n+@@\n+- strbuf_addstr(E, SB.buf);\n++ strbuf_addbuf(E, &SB);\n+\n+@@\n+expression E;\n+struct strbuf *SBP;\n+@@\n+- strbuf_addstr(E, SBP->buf);\n++ strbuf_addbuf(E, SBP);\n+\n @@\n expression E1, E2;\n format F =~ \"s\";\n-- \n2.20.1.642.gc55a771460\n\n"}]}