patch 8.2.0212: missing search/substitute pattern hardly tested

Problem:    Missing search/substitute pattern hardly tested.
Solution:   Add test_clear_search_pat() and tests. (Yegappan Lakshmanan,
            closes #5579)
diff --git a/src/evalfunc.c b/src/evalfunc.c
index a79cc89..aac8ee9 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -810,6 +810,7 @@
 #endif
     {"test_alloc_fail",	3, 3, FEARG_1,	  &t_void,	f_test_alloc_fail},
     {"test_autochdir",	0, 0, 0,	  &t_void,	f_test_autochdir},
+    {"test_clear_search_pat",	0, 0, 0,  &t_void,	f_test_clear_search_pat},
     {"test_feedinput",	1, 1, FEARG_1,	  &t_void,	f_test_feedinput},
     {"test_garbagecollect_now",	0, 0, 0,  &t_void,	f_test_garbagecollect_now},
     {"test_garbagecollect_soon", 0, 0, 0, &t_void,	f_test_garbagecollect_soon},
diff --git a/src/proto/regexp.pro b/src/proto/regexp.pro
index 01f1fff..5e8e1a5 100644
--- a/src/proto/regexp.pro
+++ b/src/proto/regexp.pro
@@ -12,6 +12,7 @@
 regprog_T *vim_regcomp(char_u *expr_arg, int re_flags);
 void vim_regfree(regprog_T *prog);
 void free_regexp_stuff(void);
+void free_regexp_prev_sub(void);
 int regprog_in_use(regprog_T *prog);
 int vim_regexec_prog(regprog_T **prog, int ignore_case, char_u *line, colnr_T col);
 int vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col);
diff --git a/src/proto/search.pro b/src/proto/search.pro
index e6ac11b..3b46c06 100644
--- a/src/proto/search.pro
+++ b/src/proto/search.pro
@@ -9,6 +9,7 @@
 void save_last_search_pattern(void);
 void restore_last_search_pattern(void);
 char_u *last_search_pattern(void);
+void free_last_pat(int idx);
 int ignorecase(char_u *pat);
 int ignorecase_opt(char_u *pat, int ic_in, int scs);
 int pat_has_uppercase(char_u *pat);
diff --git a/src/proto/testing.pro b/src/proto/testing.pro
index 2b001a5..209abf3 100644
--- a/src/proto/testing.pro
+++ b/src/proto/testing.pro
@@ -13,6 +13,7 @@
 void f_assert_true(typval_T *argvars, typval_T *rettv);
 void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
 void f_test_autochdir(typval_T *argvars, typval_T *rettv);
+void f_test_clear_search_pat(typval_T *argvars, typval_T *rettv);
 void f_test_feedinput(typval_T *argvars, typval_T *rettv);
 void f_test_getvalue(typval_T *argvars, typval_T *rettv);
 void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
diff --git a/src/regexp.c b/src/regexp.c
index ef3896c..7aeddb8 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -2663,6 +2663,15 @@
 }
 #endif
 
+/*
+ * Free the previously used substitute search pattern.
+ */
+    void
+free_regexp_prev_sub(void)
+{
+    VIM_CLEAR(reg_prev_sub);
+}
+
 #ifdef FEAT_EVAL
     static void
 report_re_switch(char_u *pat)
diff --git a/src/search.c b/src/search.c
index 3b310dc..0891557 100644
--- a/src/search.c
+++ b/src/search.c
@@ -380,6 +380,12 @@
 }
 #endif
 
+    void
+free_last_pat(int idx)
+{
+    VIM_CLEAR(spats[idx].pat);
+}
+
 /*
  * Return TRUE when case should be ignored for search pattern "pat".
  * Uses the 'ignorecase' and 'smartcase' options.
diff --git a/src/testdir/test_quickfix.vim b/src/testdir/test_quickfix.vim
index 129b1cf..79ad82d 100644
--- a/src/testdir/test_quickfix.vim
+++ b/src/testdir/test_quickfix.vim
@@ -2718,6 +2718,10 @@
   call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
   call assert_equal([], getbufinfo('Xtestfile2'))
 
+  " Test with the last search pattern not set
+  call test_clear_search_pat()
+  call assert_fails('Xvimgrep // *', 'E35:')
+
   call delete('Xtestfile1')
   call delete('Xtestfile2')
 endfunc
diff --git a/src/testdir/test_search.vim b/src/testdir/test_search.vim
index 89ca6e1..69d0073 100644
--- a/src/testdir/test_search.vim
+++ b/src/testdir/test_search.vim
@@ -1455,3 +1455,44 @@
   set t_PE=
   exe "norm /\x80PS"
 endfunc
+
+" Test for command failures when the last search pattern is not set.
+func Test_search_with_no_last_pat()
+  call test_clear_search_pat()
+  call assert_fails("normal i\<C-R>/\e", 'E35:')
+  call assert_fails("exe '/'", 'E35:')
+  call assert_fails("exe '?'", 'E35:')
+  call assert_fails("/", 'E35:')
+  call assert_fails("?", 'E35:')
+  call assert_fails("normal n", 'E35:')
+  call assert_fails("normal N", 'E35:')
+  call assert_fails("normal gn", 'E35:')
+  call assert_fails("normal gN", 'E35:')
+  call assert_fails("normal cgn", 'E35:')
+  call assert_fails("normal cgN", 'E35:')
+  let p = []
+  let p = @/
+  call assert_equal('', p)
+  call assert_fails("normal :\<C-R>/", 'E35:')
+  call assert_fails("//p", 'E35:')
+  call assert_fails(";//p", 'E35:')
+  call assert_fails("??p", 'E35:')
+  call assert_fails(";??p", 'E35:')
+  call assert_fails('g//p', 'E476:')
+  call assert_fails('v//p', 'E476:')
+endfunc
+
+" Test for using tilde (~) atom in search. This should use the last used
+" substitute pattern
+func Test_search_tilde_pat()
+  call test_clear_search_pat()
+  set regexpengine=1
+  call assert_fails('exe "normal /~\<CR>"', 'E33:')
+  call assert_fails('exe "normal ?~\<CR>"', 'E33:')
+  set regexpengine=2
+  call assert_fails('exe "normal /~\<CR>"', 'E383:')
+  call assert_fails('exe "normal ?~\<CR>"', 'E383:')
+  set regexpengine&
+endfunc
+
+" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_sort.vim b/src/testdir/test_sort.vim
index b7c68f0..f6ad87a 100644
--- a/src/testdir/test_sort.vim
+++ b/src/testdir/test_sort.vim
@@ -1383,6 +1383,8 @@
   call setline(1, ['3b', '1c', '2a'])
   sort //
   call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
+  call test_clear_search_pat()
+  call assert_fails('sort //', 'E35:')
   close!
 endfunc
 
diff --git a/src/testdir/test_substitute.vim b/src/testdir/test_substitute.vim
index 4db7b3e..acc59cf 100644
--- a/src/testdir/test_substitute.vim
+++ b/src/testdir/test_substitute.vim
@@ -803,4 +803,19 @@
   close!
 endfunc
 
+" Test for command failures when the last substitute pattern is not set.
+func Test_sub_with_no_last_pat()
+  call test_clear_search_pat()
+  call assert_fails('~', 'E33:')
+  call assert_fails('s//abc/g', 'E476:')
+  call assert_fails('s\/bar', 'E476:')
+  call assert_fails('s\&bar&', 'E476:')
+
+  call test_clear_search_pat()
+  let save_cpo = &cpo
+  set cpo+=/
+  call assert_fails('s/abc/%/', 'E33:')
+  let &cpo = save_cpo
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testing.c b/src/testing.c
index 5dacf07..c2b382b 100644
--- a/src/testing.c
+++ b/src/testing.c
@@ -632,6 +632,19 @@
 }
 
 /*
+ * "test_clear_search_pat()"
+ * Free the last search and substitute patterns
+ */
+    void
+f_test_clear_search_pat(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
+{
+    free_last_pat(RE_SUBST);
+    free_last_pat(RE_SEARCH);
+    set_old_sub(NULL);
+    free_regexp_prev_sub();
+}
+
+/*
  * "test_feedinput()"
  */
     void
diff --git a/src/version.c b/src/version.c
index a482b03..af6ecdf 100644
--- a/src/version.c
+++ b/src/version.c
@@ -743,6 +743,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    212,
+/**/
     211,
 /**/
     210,