]> git.ipfire.org Git - thirdparty/systemd.git/blob - coccinelle/strempty.cocci
coccinelle: convert hashmap_size() == 0 or friends
[thirdparty/systemd.git] / coccinelle / strempty.cocci
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 @@
3 /* Avoid running this transformation on the strempty function itself and
4 * on the "make_expression" macro in src/libsystemd/sd-bus/bus-convenience.c.
5 * As Coccinelle's Location object doesn't support macro "detection", use
6 * a pretty horrifying combo of specifying a file and a special "something_else"
7 * position element, which is, apparently, the default value of
8 * "current_element" before it's set (according to the source code), thus
9 * matching any "top level" position, including macros. Let's hope we never
10 * introduce a function called "something_else"...
11 */
12 position p : script:python() {
13 not (p[0].current_element == "strempty" or
14 (p[0].file == "src/libsystemd/sd-bus/bus-convenience.c" and
15 p[0].current_element == "something_else"))
16 };
17 expression s;
18 @@
19 (
20 - s@p ?: ""
21 + strempty(s)
22 |
23 - s@p ? s : ""
24 + strempty(s)
25 )
26
27 @@
28 position p : script:python() { p[0].current_element != "strempty" };
29 expression s;
30 @@
31 - if (!s@p)
32 - s = "";
33 + s = strempty(s);
34
35 @@
36 position p : script:python() { p[0].current_element != "strnull" };
37 expression s;
38 @@
39 (
40 - s@p ?: "(null)"
41 + strnull(s)
42 |
43 - s@p ? s : "(null)"
44 + strnull(s)
45 )
46
47 @@
48 position p : script:python() { p[0].current_element != "strnull" };
49 expression s;
50 @@
51 - if (!s@p)
52 - s = "(null)";
53 + s = strnull(s);
54
55 @@
56 position p : script:python() { p[0].current_element != "strna" };
57 expression s;
58 @@
59 (
60 - s@p ?: "n/a"
61 + strna(s)
62 |
63 - s@p ? s : "n/a"
64 + strna(s)
65 )
66
67 @@
68 position p : script:python() { p[0].current_element != "strna" };
69 expression s;
70 @@
71 - if (!s@p)
72 - s = "n/a";
73 + s = strna(s);