patch 8.0.0593: duplication of code for adding a list or dict return value

Problem:    Duplication of code for adding a list or dict return value.
Solution:   Add rettv_dict_set() and rettv_list_set(). (Yegappan Lakshmanan)
diff --git a/src/dict.c b/src/dict.c
index a26419b..c13e7a4 100644
--- a/src/dict.c
+++ b/src/dict.c
@@ -59,14 +59,24 @@
     if (d == NULL)
 	return FAIL;
 
-    rettv->vval.v_dict = d;
-    rettv->v_type = VAR_DICT;
+    rettv_dict_set(rettv, d);
     rettv->v_lock = 0;
-    ++d->dv_refcount;
     return OK;
 }
 
 /*
+ * Set a dictionary as the return value
+ */
+    void
+rettv_dict_set(typval_T *rettv, dict_T *d)
+{
+    rettv->v_type = VAR_DICT;
+    rettv->vval.v_dict = d;
+    if (d != NULL)
+	++d->dv_refcount;
+}
+
+/*
  * Free a Dictionary, including all non-container items it contains.
  * Ignores the reference count.
  */
@@ -646,11 +656,7 @@
 
     *arg = skipwhite(*arg + 1);
     if (evaluate)
-    {
-	rettv->v_type = VAR_DICT;
-	rettv->vval.v_dict = d;
-	++d->dv_refcount;
-    }
+	rettv_dict_set(rettv, d);
 
     return OK;
 }
diff --git a/src/eval.c b/src/eval.c
index 1313cef..401171c 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -4665,9 +4665,7 @@
 			item = item->li_next;
 		    }
 		    clear_tv(rettv);
-		    rettv->v_type = VAR_LIST;
-		    rettv->vval.v_list = l;
-		    ++l->lv_refcount;
+		    rettv_list_set(rettv, l);
 		}
 		else
 		{
@@ -8486,9 +8484,7 @@
 
 		    if (opts != NULL)
 		    {
-			rettv->v_type = VAR_DICT;
-			rettv->vval.v_dict = opts;
-			++opts->dv_refcount;
+			rettv_dict_set(rettv, opts);
 			done = TRUE;
 		    }
 		}
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 45c43f6..b16b260 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -3005,8 +3005,7 @@
 	    && get_tv_number_chk(&argvars[2], &error)
 	    && !error)
     {
-	rettv->v_type = VAR_LIST;
-	rettv->vval.v_list = NULL;
+	rettv_list_set(rettv, NULL);
     }
 
     s = get_tv_string(&argvars[0]);
@@ -3909,12 +3908,7 @@
 		}
 	    }
 	    else if (STRCMP(what, "dict") == 0)
-	    {
-		rettv->v_type = VAR_DICT;
-		rettv->vval.v_dict = pt->pt_dict;
-		if (pt->pt_dict != NULL)
-		    ++pt->pt_dict->dv_refcount;
-	    }
+		rettv_dict_set(rettv, pt->pt_dict);
 	    else if (STRCMP(what, "args") == 0)
 	    {
 		rettv->v_type = VAR_LIST;
@@ -4214,9 +4208,7 @@
 
 		if (opts != NULL)
 		{
-		    rettv->v_type = VAR_DICT;
-		    rettv->vval.v_dict = opts;
-		    ++opts->dv_refcount;
+		    rettv_dict_set(rettv, opts);
 		    done = TRUE;
 		}
 	    }
@@ -5372,8 +5364,7 @@
 	{
 	    if (get_tv_number_chk(&argvars[2], &error))
 	    {
-		rettv->v_type = VAR_LIST;
-		rettv->vval.v_list = NULL;
+		rettv_list_set(rettv, NULL);
 	    }
 	    if (argvars[3].v_type != VAR_UNKNOWN
 				    && get_tv_number_chk(&argvars[3], &error))
@@ -5429,8 +5420,7 @@
 	{
 	    if (get_tv_number_chk(&argvars[3], &error))
 	    {
-		rettv->v_type = VAR_LIST;
-		rettv->vval.v_list = NULL;
+		rettv_list_set(rettv, NULL);
 	    }
 	    if (argvars[4].v_type != VAR_UNKNOWN
 				    && get_tv_number_chk(&argvars[4], &error))
@@ -9152,9 +9142,7 @@
 	    list_append(l, li);
 	    li = ni;
 	}
-	rettv->vval.v_list = l;
-	rettv->v_type = VAR_LIST;
-	++l->lv_refcount;
+	rettv_list_set(rettv, l);
 	l->lv_idx = l->lv_len - l->lv_idx - 1;
     }
 }
@@ -10742,9 +10730,7 @@
 	     (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
 									TRUE))
 	    goto theend;
-	rettv->vval.v_list = l;
-	rettv->v_type = VAR_LIST;
-	++l->lv_refcount;
+	rettv_list_set(rettv, l);
 
 	len = list_len(l);
 	if (len <= 1)
@@ -11832,8 +11818,7 @@
     char_u	str[NUMBUFLEN];
 #endif
 
-    rettv->v_type = VAR_LIST;
-    rettv->vval.v_list = NULL;
+    rettv_list_set(rettv, NULL);
 
 #if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
     lnum = get_tv_lnum(argvars);		/* -1 on type error */
@@ -11890,8 +11875,7 @@
     int		id;
 #endif
 
-    rettv->v_type = VAR_LIST;
-    rettv->vval.v_list = NULL;
+    rettv_list_set(rettv, NULL);
 
 #ifdef FEAT_SYN_HL
     lnum = get_tv_lnum(argvars);		/* -1 on type error */
@@ -12057,9 +12041,7 @@
 	    list_append(list, li);
 	}
 
-	++list->lv_refcount;
-	rettv->v_type = VAR_LIST;
-	rettv->vval.v_list = list;
+	rettv_list_set(rettv, list);
 	list = NULL;
     }
     else
@@ -12465,8 +12447,7 @@
     static void
 f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
 {
-    rettv->v_type = VAR_DICT;
-    rettv->vval.v_dict = NULL;
+    rettv_dict_set(rettv, NULL);
 }
 
 #ifdef FEAT_JOB_CHANNEL
@@ -12481,8 +12462,7 @@
     static void
 f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
 {
-    rettv->v_type = VAR_LIST;
-    rettv->vval.v_list = NULL;
+    rettv_list_set(rettv, NULL);
 }
 
     static void
diff --git a/src/if_perl.xs b/src/if_perl.xs
index 076c5c6..2bf72c5 100644
--- a/src/if_perl.xs
+++ b/src/if_perl.xs
@@ -1136,9 +1136,7 @@
 		}
 	    }
 
-	    list->lv_refcount++;
-	    rettv->v_type	= VAR_LIST;
-	    rettv->vval.v_list	= list;
+	    rettv_list_set(rettv, list);
 	    break;
 	}
 	case SVt_PVHV:	/* dictionary */
@@ -1192,9 +1190,7 @@
 		}
 	    }
 
-	    dict->dv_refcount++;
-	    rettv->v_type	= VAR_DICT;
-	    rettv->vval.v_dict	= dict;
+	    rettv_dict_set(rettv, dict);
 	    break;
 	}
 	default:	/* not convertible */
diff --git a/src/list.c b/src/list.c
index 50d38ff..2fccbae 100644
--- a/src/list.c
+++ b/src/list.c
@@ -97,14 +97,24 @@
     if (l == NULL)
 	return FAIL;
 
-    rettv->vval.v_list = l;
-    rettv->v_type = VAR_LIST;
     rettv->v_lock = 0;
-    ++l->lv_refcount;
+    rettv_list_set(rettv, l);
     return OK;
 }
 
 /*
+ * Set a list as the return value
+ */
+    void
+rettv_list_set(typval_T *rettv, list_T *l)
+{
+    rettv->v_type = VAR_LIST;
+    rettv->vval.v_list = l;
+    if (l != NULL)
+	++l->lv_refcount;
+}
+
+/*
  * Unreference a list: decrement the reference count and free it when it
  * becomes zero.
  */
@@ -875,11 +885,7 @@
 
     *arg = skipwhite(*arg + 1);
     if (evaluate)
-    {
-	rettv->v_type = VAR_LIST;
-	rettv->vval.v_list = l;
-	++l->lv_refcount;
-    }
+	rettv_list_set(rettv, l);
 
     return OK;
 }
diff --git a/src/proto/dict.pro b/src/proto/dict.pro
index 61f4dfa..2a76263 100644
--- a/src/proto/dict.pro
+++ b/src/proto/dict.pro
@@ -1,6 +1,7 @@
 /* dict.c */
 dict_T *dict_alloc(void);
 int rettv_dict_alloc(typval_T *rettv);
+void rettv_dict_set(typval_T *rettv, dict_T *d);
 void dict_unref(dict_T *d);
 int dict_free_nonref(int copyID);
 void dict_free_items(int copyID);
diff --git a/src/proto/list.pro b/src/proto/list.pro
index 56f0ddc..fe54bab 100644
--- a/src/proto/list.pro
+++ b/src/proto/list.pro
@@ -4,6 +4,7 @@
 void list_fix_watch(list_T *l, listitem_T *item);
 list_T *list_alloc(void);
 int rettv_list_alloc(typval_T *rettv);
+void rettv_list_set(typval_T *rettv, list_T *l);
 void list_unref(list_T *l);
 int list_free_nonref(int copyID);
 void list_free_items(int copyID);
diff --git a/src/version.c b/src/version.c
index fb0d9a5..9e6eeac 100644
--- a/src/version.c
+++ b/src/version.c
@@ -765,6 +765,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    593,
+/**/
     592,
 /**/
     591,