]> granicus.if.org Git - vim/commitdiff
patch 8.2.4431: unnecessary condition when assigning to a variable v8.2.4431
authorBram Moolenaar <Bram@vim.org>
Mon, 21 Feb 2022 15:59:11 +0000 (15:59 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 21 Feb 2022 15:59:11 +0000 (15:59 +0000)
Problem:    Unnecessary condition when assigning to a variable.
Solution:   Remove the condition.

src/evalvars.c
src/version.c

index dabd49b675a4d260dfba2086b03e7c7b257b3179..96d60a9595ba9c4cfee0aa8b46e72bd2ffdca701 100644 (file)
@@ -3409,7 +3409,7 @@ set_var_const(
     type_T     *type = type_arg;
     typval_T   bool_tv;
     dictitem_T *di;
-    typval_T   *dest_tv = NULL;
+    typval_T   *dest_tv;
     char_u     *varname;
     char_u     *name_tofree = NULL;
     hashtab_T  *ht = NULL;
@@ -3503,185 +3503,181 @@ set_var_const(
        }
     }
 
-    if (dest_tv == NULL)
-    {
-       // Search in parent scope which is possible to reference from lambda
-       if (di == NULL)
-           di = find_var_in_scoped_ht(name, TRUE);
+    // Search in parent scope which is possible to reference from lambda
+    if (di == NULL)
+       di = find_var_in_scoped_ht(name, TRUE);
 
-       if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
-                                     && var_wrong_func_name(name, di == NULL))
-           goto failed;
+    if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
+                                 && var_wrong_func_name(name, di == NULL))
+       goto failed;
 
-       if (need_convert_to_bool(type, tv))
-       {
-           // Destination is a bool and the value is not, but it can be
-           // converted.
-           CLEAR_FIELD(bool_tv);
-           bool_tv.v_type = VAR_BOOL;
-           bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
-           tv = &bool_tv;
-       }
+    if (need_convert_to_bool(type, tv))
+    {
+       // Destination is a bool and the value is not, but it can be
+       // converted.
+       CLEAR_FIELD(bool_tv);
+       bool_tv.v_type = VAR_BOOL;
+       bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
+       tv = &bool_tv;
+    }
 
-       if (di != NULL)
+    if (di != NULL)
+    {
+       // Item already exists.  Allowed to replace when reloading.
+       if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
        {
-           // Item already exists.  Allowed to replace when reloading.
-           if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
+           if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
+                                        && (flags & ASSIGN_FOR_LOOP) == 0)
            {
-               if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
-                                            && (flags & ASSIGN_FOR_LOOP) == 0)
-               {
-                   emsg(_(e_cannot_modify_existing_variable));
-                   goto failed;
-               }
-
-               if (is_script_local && vim9script
-                             && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
-               {
-                   semsg(_(e_redefining_script_item_str), name);
-                   goto failed;
-               }
-
-               if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
-               {
-                   where_T where = WHERE_INIT;
-                   svar_T  *sv = find_typval_in_script(&di->di_tv, sid);
-
-                   if (sv != NULL)
-                   {
-                       // check the type and adjust to bool if needed
-                       where.wt_index = var_idx;
-                       where.wt_variable = TRUE;
-                       if (check_script_var_type(sv, tv, name, where) == FAIL)
-                           goto failed;
-                       if (type == NULL)
-                           type = sv->sv_type;
-                   }
-               }
-
-               if ((flags & ASSIGN_FOR_LOOP) == 0
-                                    && var_check_permission(di, name) == FAIL)
-                   goto failed;
+               emsg(_(e_cannot_modify_existing_variable));
+               goto failed;
            }
-           else
+
+           if (is_script_local && vim9script
+                         && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
            {
-               // can only redefine once
-               di->di_flags &= ~DI_FLAGS_RELOAD;
-
-               // A Vim9 script-local variable is also present in sn_all_vars
-               // and sn_var_vals.  It may set "type" from "tv".
-               if (var_in_vim9script || var_in_autoload)
-                   update_vim9_script_var(FALSE, di,
-                           var_in_autoload ? name : di->di_key, flags,
-                           tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
+               semsg(_(e_redefining_script_item_str), name);
+               goto failed;
            }
 
-           // existing variable, need to clear the value
-
-           // Handle setting internal di: variables separately where needed to
-           // prevent changing the type.
-           if (ht == &vimvarht)
+           if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
            {
-               if (di->di_tv.v_type == VAR_STRING)
-               {
-                   VIM_CLEAR(di->di_tv.vval.v_string);
-                   if (copy || tv->v_type != VAR_STRING)
-                   {
-                       char_u *val = tv_get_string(tv);
+               where_T where = WHERE_INIT;
+               svar_T  *sv = find_typval_in_script(&di->di_tv, sid);
 
-                       // Careful: when assigning to v:errmsg and
-                       // tv_get_string() causes an error message the variable
-                       // will already be set.
-                       if (di->di_tv.vval.v_string == NULL)
-                           di->di_tv.vval.v_string = vim_strsave(val);
-                   }
-                   else
-                   {
-                       // Take over the string to avoid an extra alloc/free.
-                       di->di_tv.vval.v_string = tv->vval.v_string;
-                       tv->vval.v_string = NULL;
-                   }
-                   goto failed;
-               }
-               else if (di->di_tv.v_type == VAR_NUMBER)
-               {
-                   di->di_tv.vval.v_number = tv_get_number(tv);
-                   if (STRCMP(varname, "searchforward") == 0)
-                       set_search_direction(di->di_tv.vval.v_number
-                                                                 ? '/' : '?');
-#ifdef FEAT_SEARCH_EXTRA
-                   else if (STRCMP(varname, "hlsearch") == 0)
-                   {
-                       no_hlsearch = !di->di_tv.vval.v_number;
-                       redraw_all_later(SOME_VALID);
-                   }
-#endif
-                   goto failed;
-               }
-               else if (di->di_tv.v_type != tv->v_type)
+               if (sv != NULL)
                {
-                   semsg(_(e_setting_str_to_value_with_wrong_type), name);
-                   goto failed;
+                   // check the type and adjust to bool if needed
+                   where.wt_index = var_idx;
+                   where.wt_variable = TRUE;
+                   if (check_script_var_type(sv, tv, name, where) == FAIL)
+                       goto failed;
+                   if (type == NULL)
+                       type = sv->sv_type;
                }
            }
 
-           clear_tv(&di->di_tv);
+           if ((flags & ASSIGN_FOR_LOOP) == 0
+                                && var_check_permission(di, name) == FAIL)
+               goto failed;
        }
        else
        {
-           // Item not found, check if a function already exists.
-           if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
-                  && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
+           // can only redefine once
+           di->di_flags &= ~DI_FLAGS_RELOAD;
+
+           // A Vim9 script-local variable is also present in sn_all_vars
+           // and sn_var_vals.  It may set "type" from "tv".
+           if (var_in_vim9script || var_in_autoload)
+               update_vim9_script_var(FALSE, di,
+                       var_in_autoload ? name : di->di_key, flags,
+                       tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
+       }
+
+       // existing variable, need to clear the value
+
+       // Handle setting internal di: variables separately where needed to
+       // prevent changing the type.
+       if (ht == &vimvarht)
+       {
+           if (di->di_tv.v_type == VAR_STRING)
            {
-               semsg(_(e_redefining_script_item_str), name);
+               VIM_CLEAR(di->di_tv.vval.v_string);
+               if (copy || tv->v_type != VAR_STRING)
+               {
+                   char_u *val = tv_get_string(tv);
+
+                   // Careful: when assigning to v:errmsg and
+                   // tv_get_string() causes an error message the variable
+                   // will already be set.
+                   if (di->di_tv.vval.v_string == NULL)
+                       di->di_tv.vval.v_string = vim_strsave(val);
+               }
+               else
+               {
+                   // Take over the string to avoid an extra alloc/free.
+                   di->di_tv.vval.v_string = tv->vval.v_string;
+                   tv->vval.v_string = NULL;
+               }
                goto failed;
            }
-
-           // add a new variable
-           if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
+           else if (di->di_tv.v_type == VAR_NUMBER)
            {
-               semsg(_(e_unknown_variable_str), name);
+               di->di_tv.vval.v_number = tv_get_number(tv);
+               if (STRCMP(varname, "searchforward") == 0)
+                   set_search_direction(di->di_tv.vval.v_number
+                                                             ? '/' : '?');
+#ifdef FEAT_SEARCH_EXTRA
+               else if (STRCMP(varname, "hlsearch") == 0)
+               {
+                   no_hlsearch = !di->di_tv.vval.v_number;
+                   redraw_all_later(SOME_VALID);
+               }
+#endif
                goto failed;
            }
-
-           // Can't add "v:" or "a:" variable.
-           if (ht == &vimvarht || ht == get_funccal_args_ht())
+           else if (di->di_tv.v_type != tv->v_type)
            {
-               semsg(_(e_illegal_variable_name_str), name);
+               semsg(_(e_setting_str_to_value_with_wrong_type), name);
                goto failed;
            }
+       }
 
-           // Make sure the variable name is valid.  In Vim9 script an
-           // autoload variable must be prefixed with "g:" unless in an
-           // autoload script.
-           if (!valid_varname(varname, -1, !vim9script
-                           || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
-               goto failed;
+       clear_tv(&di->di_tv);
+    }
+    else
+    {
+       // Item not found, check if a function already exists.
+       if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
+              && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
+       {
+           semsg(_(e_redefining_script_item_str), name);
+           goto failed;
+       }
 
-           di = alloc(sizeof(dictitem_T) + STRLEN(varname));
-           if (di == NULL)
-               goto failed;
-           STRCPY(di->di_key, varname);
-           if (hash_add(ht, DI2HIKEY(di)) == FAIL)
-           {
-               vim_free(di);
-               goto failed;
-           }
-           di->di_flags = DI_FLAGS_ALLOC;
-           if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
-               di->di_flags |= DI_FLAGS_LOCK;
+       // add a new variable
+       if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
+       {
+           semsg(_(e_unknown_variable_str), name);
+           goto failed;
+       }
 
-           // A Vim9 script-local variable is also added to sn_all_vars and
-           // sn_var_vals. It may set "type" from "tv".
-           if (var_in_vim9script || var_in_autoload)
-               update_vim9_script_var(TRUE, di,
-                       var_in_autoload ? name : di->di_key, flags,
-                             tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
+       // Can't add "v:" or "a:" variable.
+       if (ht == &vimvarht || ht == get_funccal_args_ht())
+       {
+           semsg(_(e_illegal_variable_name_str), name);
+           goto failed;
        }
 
-       dest_tv = &di->di_tv;
+       // Make sure the variable name is valid.  In Vim9 script an
+       // autoload variable must be prefixed with "g:" unless in an
+       // autoload script.
+       if (!valid_varname(varname, -1, !vim9script
+                       || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
+           goto failed;
+
+       di = alloc(sizeof(dictitem_T) + STRLEN(varname));
+       if (di == NULL)
+           goto failed;
+       STRCPY(di->di_key, varname);
+       if (hash_add(ht, DI2HIKEY(di)) == FAIL)
+       {
+           vim_free(di);
+           goto failed;
+       }
+       di->di_flags = DI_FLAGS_ALLOC;
+       if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
+           di->di_flags |= DI_FLAGS_LOCK;
+
+       // A Vim9 script-local variable is also added to sn_all_vars and
+       // sn_var_vals. It may set "type" from "tv".
+       if (var_in_vim9script || var_in_autoload)
+           update_vim9_script_var(TRUE, di,
+                   var_in_autoload ? name : di->di_key, flags,
+                         tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
     }
 
+    dest_tv = &di->di_tv;
     if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
        copy_tv(tv, dest_tv);
     else
index f3b54f0f5dc55ab5c49264263b14db99bd05ecb5..03f2bd0fd1d2e12880dbb4174788bd22f1cfff86 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4431,
 /**/
     4430,
 /**/