Revert "xpath OPTIMIZE dead code and unused error removed"

This reverts commit 7e002860aab25e898ccc4965b5694c4e77464317.
This commit introduced ABI change, so it will be merged later,
with a new release.
diff --git a/src/common.h b/src/common.h
index 45d894a..6dedeee 100644
--- a/src/common.h
+++ b/src/common.h
@@ -224,6 +224,7 @@
     LYE_NOLEAFREF,
     LYE_NOMANDCHOICE,
 
+    LYE_XPATH_INSNODE,
     LYE_XPATH_INTOK,
     LYE_XPATH_EOF,
     LYE_XPATH_INOP_1,
diff --git a/src/libyang.h.in b/src/libyang.h.in
index c38a7b3..36cb4ad 100644
--- a/src/libyang.h.in
+++ b/src/libyang.h.in
@@ -1723,6 +1723,7 @@
     LYVE_NOLEAFREF,    /**< leaf pointed to by leafref does not exist (data) */
     LYVE_NOMANDCHOICE, /**< no mandatory choice case branch exists (data) */
 
+    LYVE_XPATH_INSNODE,/**< schema node not found */
     LYVE_XPATH_INTOK,  /**< unexpected XPath token */
     LYVE_XPATH_EOF,    /**< unexpected end of an XPath expression */
     LYVE_XPATH_INOP,   /**< invalid XPath operation operands */
diff --git a/src/log.c b/src/log.c
index b25de24..1456c0e 100644
--- a/src/log.c
+++ b/src/log.c
@@ -302,6 +302,7 @@
 /* LYE_NOLEAFREF */    "Leafref \"%s\" of value \"%s\" points to a non-existing leaf.",
 /* LYE_NOMANDCHOICE */ "Mandatory choice \"%s\" missing a case branch.",
 
+/* LYE_XPATH_INSNODE */"Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
 /* LYE_XPATH_INTOK */  "Unexpected XPath token %s (%.15s).",
 /* LYE_XPATH_EOF */    "Unexpected XPath expression end.",
 /* LYE_XPATH_INOP_1 */ "Cannot apply XPath operation %s on %s.",
@@ -396,6 +397,7 @@
     LYVE_NOLEAFREF,    /* LYE_NOLEAFREF */
     LYVE_NOMANDCHOICE, /* LYE_NOMANDCHOICE */
 
+    LYVE_XPATH_INSNODE,/* LYE_XPATH_INSNODE */
     LYVE_XPATH_INTOK,  /* LYE_XPATH_INTOK */
     LYVE_XPATH_EOF,    /* LYE_XPATH_EOF */
     LYVE_XPATH_INOP,   /* LYE_XPATH_INOP_1 */
diff --git a/src/xpath.c b/src/xpath.c
index 050ff0e..0f65d62 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -6374,7 +6374,7 @@
                                              exp->tok_len[*exp_idx], options);
                 }
             } else {
-                if (set && (options & LYXP_SNODE_ALL)) {
+                if (set && (set->type == LYXP_SET_SNODE_SET)) {
                     rc = moveto_snode(set, (struct lys_node *)cur_node, &exp->expr[exp->expr_pos[*exp_idx]],
                                       exp->tok_len[*exp_idx], options);
                 } else {
@@ -6390,11 +6390,20 @@
                     }
                 }
                 if (i == -1) {
-                    path = lys_path((struct lys_node *)cur_node);
-                    LOGWRN("Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
-                           exp->tok_len[*exp_idx], &exp->expr[exp->expr_pos[*exp_idx]],
-                           exp->expr_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
-                    free(path);
+                    if (options & LYXP_SNODE_ALL) {
+                        path = lys_path((struct lys_node *)cur_node);
+                        LOGWRN("Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
+                               exp->tok_len[*exp_idx], &exp->expr[exp->expr_pos[*exp_idx]],
+                               exp->expr_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
+                        free(path);
+                    } else {
+                        path = lyd_path(cur_node);
+                        LOGVAL(LYE_XPATH_INSNODE, LY_VLOG_NONE, NULL,
+                               exp->tok_len[*exp_idx], &exp->expr[exp->expr_pos[*exp_idx]],
+                               exp->expr_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
+                        free(path);
+                        return -1;
+                    }
                 }
             }
         }