]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0370: duplicate code with literal string_T assignment v9.2.0370
authorHirohito Higashi <h.east.727@gmail.com>
Mon, 20 Apr 2026 14:43:52 +0000 (14:43 +0000)
committerChristian Brabandt <cb@256bit.org>
Mon, 20 Apr 2026 14:43:52 +0000 (14:43 +0000)
Problem:  Duplicate code with literal string_T assignment
Solution: Add STR_LITERAL_SET() macro for string_T literal assignment
          (Hirohito Higashi).

Previously, assigning a string literal to a string_T variable required
two lines that repeated the literal:

    s.string = (char_u *)"open";
    s.length = STRLEN_LITERAL("open");

Writing the literal twice is error-prone -- a typo in one of them
leaves the pointer and the cached length out of sync.

Add a STR_LITERAL_SET() macro in macros.h so that the assignment can
be written in one statement with the literal appearing only once:

    STR_LITERAL_SET(s, "open");

Replace all occurrences of the two-line pattern across the codebase
with the new macro.

No functional change.

related: #19999
related: #20023
closes:  #20025

Signed-off-by: Hirohito Higashi <h.east.727@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/autocmd.c
src/channel.c
src/eval.c
src/fileio.c
src/macros.h
src/os_win32.c
src/scriptfile.c
src/strings.c
src/textprop.c
src/version.c
src/vim9type.c

index 99d6b3c92479746a012e1f6cd668df05aae7459a..78965529dbe883ac96dca3f92fede5e5a4e1b804 100644 (file)
@@ -3444,10 +3444,7 @@ f_autocmd_get(typval_T *argvars, typval_T *rettv)
 
            group_name.string = get_augroup_name(NULL, ap->group);
            if (group_name.string == NULL)
-           {
-               group_name.string = (char_u *)"";
-               group_name.length = 0;
-           }
+               STR_LITERAL_SET(group_name, "");
            else
                group_name.length = STRLEN(group_name.string);
 
index 56878458eae6d7e1d913c72861362522540ea06f..dc8645b060d9480b12ea8f72b4bb1ff41abcded6 100644 (file)
@@ -3677,52 +3677,36 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
 
     STRCPY(namebuf + tail, "status");
     if (chanpart->ch_fd != INVALID_FD)
-    {
-       s.string = (char_u *)"open";
-       s.length = STRLEN_LITERAL("open");
-    }
+       STR_LITERAL_SET(s, "open");
     else if (channel_has_readahead(channel, part))
-    {
-       s.string = (char_u *)"buffered";
-       s.length = STRLEN_LITERAL("buffered");
-    }
+       STR_LITERAL_SET(s, "buffered");
     else
-    {
-       s.string = (char_u *)"closed";
-       s.length = STRLEN_LITERAL("closed");
-    }
+       STR_LITERAL_SET(s, "closed");
     dict_add_string_len(dict, namebuf, s.string, (int)s.length);
 
     STRCPY(namebuf + tail, "mode");
     switch (chanpart->ch_mode)
     {
        case CH_MODE_NL:
-           s.string = (char_u *)"NL";
-           s.length = STRLEN_LITERAL("NL");
+           STR_LITERAL_SET(s, "NL");
            break;
        case CH_MODE_RAW:
-           s.string = (char_u *)"RAW";
-           s.length = STRLEN_LITERAL("RAW");
+           STR_LITERAL_SET(s, "RAW");
            break;
        case CH_MODE_JSON:
-           s.string = (char_u *)"JSON";
-           s.length = STRLEN_LITERAL("JSON");
+           STR_LITERAL_SET(s, "JSON");
            break;
        case CH_MODE_JS:
-           s.string = (char_u *)"JS";
-           s.length = STRLEN_LITERAL("JS");
+           STR_LITERAL_SET(s, "JS");
            break;
        case CH_MODE_LSP:
-           s.string = (char_u *)"LSP";
-           s.length = STRLEN_LITERAL("LSP");
+           STR_LITERAL_SET(s, "LSP");
            break;
        case CH_MODE_DAP:
-           s.string = (char_u *)"DAP";
-           s.length = STRLEN_LITERAL("DAP");
+           STR_LITERAL_SET(s, "DAP");
            break;
        default:
-           s.string = (char_u *)"";
-           s.length = 0;
+           STR_LITERAL_SET(s, "");
            break;
     }
     dict_add_string_len(dict, namebuf, s.string, (int)s.length);
index 6f7c9960a29ea501e541cbdb5dc7834f1d793a7a..7d212b5b30de75af9b0f44168d53c48a99c65cff 100644 (file)
@@ -6579,15 +6579,9 @@ class_tv2string(typval_T *tv, char_u **tofree)
        class_name.string = cl->class_name.string;
        class_name.length = cl->class_name.length;
        if (IS_INTERFACE(cl))
-       {
-           s.string = (char_u *)"interface";
-           s.length = 9;
-       }
+           STR_LITERAL_SET(s, "interface");
        else if (IS_ENUM(cl))
-       {
-           s.string = (char_u *)"enum";
-           s.length = 4;
-       }
+           STR_LITERAL_SET(s, "enum");
     }
 
     rsize = s.length + 1 + class_name.length + 1;
index b023e5a0d1dcbdad77ddbd55755b6a19fccd76d8..1a7ee9b94ddeba4af5c7704431447e641a1e24a6 100644 (file)
@@ -4945,10 +4945,7 @@ create_readdirex_item(char_u *path, char_u *name)
        link = TRUE;
        ret = mch_stat(p, &st);
        if (ret < 0)
-       {
-           q.string = (char_u *)"link";
-           q.length = STRLEN_LITERAL("link");
-       }
+           STR_LITERAL_SET(q, "link");
     }
     vim_free(p);
 
@@ -4971,15 +4968,9 @@ create_readdirex_item(char_u *path, char_u *name)
        if (link)
        {
            if (S_ISDIR(st.st_mode))
-           {
-               q.string = (char_u *)"linkd";
-               q.length = STRLEN_LITERAL("linkd");
-           }
+               STR_LITERAL_SET(q, "linkd");
            else
-           {
-               q.string = (char_u *)"link";
-               q.length = STRLEN_LITERAL("link");
-           }
+               STR_LITERAL_SET(q, "link");
        }
        else
        {
@@ -4993,10 +4984,7 @@ create_readdirex_item(char_u *path, char_u *name)
 
        pw = getpwuid(st.st_uid);
        if (pw == NULL)
-       {
-           q.string = (char_u *)"";
-           q.length = 0;
-       }
+           STR_LITERAL_SET(q, "");
        else
        {
            q.string = (char_u *)pw->pw_name;
@@ -5007,10 +4995,7 @@ create_readdirex_item(char_u *path, char_u *name)
 #  if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
        gr = getgrgid(st.st_gid);
        if (gr == NULL)
-       {
-           q.string = (char_u *)"";
-           q.length = 0;
-       }
+           STR_LITERAL_SET(q, "");
        else
        {
            q.string = (char_u *)gr->gr_name;
index 5fdcfdb2a898092aeb580027e9e346d9c249d554..3b246b22006615f88808003bc861459801013809 100644 (file)
 #define STR_LITERAL_INIT(s) \
     {(char_u *)(s), STRLEN_LITERAL(s)}
 
+#define STR_LITERAL_SET(str, s) \
+    do { \
+       (str).string = (char_u *)(s); \
+       (str).length = STRLEN_LITERAL(s); \
+    } while (0)
+
 // Whether a command index indicates a user command.
 #define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
 
index e7216c1220ccc3287a880d7554cec329633ccd9c..e8f8162f1b05e63ba5a7f2ec12dec7a12b679343 100644 (file)
@@ -2730,10 +2730,7 @@ executable_exists(
     {
        pathext.string = mch_getenv("PATHEXT");
        if (pathext.string == NULL)
-       {
-           pathext.string = (char_u *)".com;.exe;.bat;.cmd";
-           pathext.length = 19;
-       }
+           STR_LITERAL_SET(pathext, ".com;.exe;.bat;.cmd");
        else
            pathext.length = STRLEN(pathext.string);
 
@@ -2774,10 +2771,7 @@ executable_exists(
 
     // Prepend single "." to pathext, it means no extension added.
     if (pathext.string == NULL)
-    {
-       pathext.string = (char_u *)".";
-       pathext.length = 1;
-    }
+       STR_LITERAL_SET(pathext, ".");
     else if (noext == TRUE)
     {
        char_u  *tmp;
@@ -2828,10 +2822,7 @@ executable_exists(
      * is an executable file.
      */
     if (pathbuf.string == NULL)
-    {
-       pathbuf.string = (char_u *)".";
-       pathbuf.length = 1;
-    }
+       STR_LITERAL_SET(pathbuf, ".");
     p = pathbuf.string;
     while (*p)
     {
index df90fe77117fc64dfc377362a748ee9714a129bf..d0735ffe448b5f0560b28adc9f4b3ddabc3f3219 100644 (file)
@@ -199,12 +199,10 @@ estack_sfile(estack_arg_T which UNUSED)
                switch (entry->es_type)
                {
                    case ETYPE_SCRIPT:
-                       type_name.string = (char_u *)"script ";
-                       type_name.length = 7;
+                       STR_LITERAL_SET(type_name, "script ");
                        break;
                    case ETYPE_UFUNC:
-                       type_name.string = (char_u *)"function ";
-                       type_name.length = 9;
+                       STR_LITERAL_SET(type_name, "function ");
                        break;
                    default:
                        break;
index 82ebdb644d21da5e9943d201c3964d62ca93b21b..ec81ee469a2b331297499b87b21b7e18586e19c2 100644 (file)
@@ -1601,10 +1601,7 @@ f_str2blob(typval_T *argvars, typval_T *rettv)
        string_T    str = {li->li_tv.vval.v_string, 0};
 
        if (str.string == NULL)
-       {
-           str.string = (char_u *)"";
-           str.length = 0;
-       }
+           STR_LITERAL_SET(str, "");
        else
            str.length = STRLEN(str.string);
 
index 782701aff184aa09a36a31addafab2468df0543a..b4aedf98d7c498ac6ff9254a2d67e990e752a6ff 100644 (file)
@@ -1734,20 +1734,11 @@ prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
 
        // text_align
        if (prop->tp_flags & TP_FLAG_ALIGN_RIGHT)
-       {
-           text_align.string = (char_u *)"right";
-           text_align.length = STRLEN_LITERAL("right");
-       }
+           STR_LITERAL_SET(text_align, "right");
        else if (prop->tp_flags & TP_FLAG_ALIGN_ABOVE)
-       {
-           text_align.string = (char_u *)"above";
-           text_align.length = STRLEN_LITERAL("above");
-       }
+           STR_LITERAL_SET(text_align, "above");
        else if (prop->tp_flags & TP_FLAG_ALIGN_BELOW)
-       {
-           text_align.string = (char_u *)"below";
-           text_align.length = STRLEN_LITERAL("below");
-       }
+           STR_LITERAL_SET(text_align, "below");
        if (text_align.string != NULL)
            dict_add_string_len(dict, "text_align",
                text_align.string, (int)text_align.length);
index 681858f70d00a63d65726c781aa49b2d2f587ac0..489ec89deebab6d93cedb709920acb4e11f4921d 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    370,
 /**/
     369,
 /**/
index 3d9282f601205c3ad036f0e25dca7659503db96f..1bd94a60cb4b4103ac171bf7456556a0e4a4e9b3 100644 (file)
@@ -2658,10 +2658,7 @@ type_name_class_or_obj(char *name, type_T *type, char **tofree)
            name = "enum";
     }
     else
-    {
-       class_name.string = (char_u *)"any";
-       class_name.length = 3;
-    }
+       STR_LITERAL_SET(class_name, "any");
 
     size_t len = STRLEN(name) + class_name.length + 3;
     *tofree = alloc(len);
@@ -2695,10 +2692,7 @@ type_name_func(type_T *type, char **tofree)
        string_T    arg_type;
 
        if (type->tt_args == NULL)
-       {
-           arg_type.string = (char_u *)"[unknown]";
-           arg_type.length = 9;
-       }
+           STR_LITERAL_SET(arg_type, "[unknown]");
        else
        {
            arg_type.string = (char_u *)type_name(type->tt_args[i], &arg_free);