branch: elpa/cider
commit 8df8f1f146552351f54138f151d157d7447beb1e
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    Guard cider-find-keyword against no keyword at point
    
    With the default `cider-prompt-for-symbol' and no prefix arg, the symbol at
    point could be nil and flow into `string-match', crashing with a raw
    `wrong-type-argument'.  Signal a clean `user-error' instead.
---
 lisp/cider-find.el       |  4 +++-
 test/cider-find-tests.el | 10 +++++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/lisp/cider-find.el b/lisp/cider-find.el
index 4b90567ee8..4d3ab03cc4 100644
--- a/lisp/cider-find.el
+++ b/lisp/cider-find.el
@@ -278,7 +278,9 @@ thing at point."
                     nil nil kw-at-point)
                  kw-at-point)))
          (before (buffer-list))
-         (result (cider--find-keyword-loc kw)))
+         (result (if kw
+                     (cider--find-keyword-loc kw)
+                   (user-error "No keyword at point"))))
     (nrepl-dbind-response result (dest dest-point)
       (if dest-point
           (cider-jump-to dest dest-point (cider--open-other-window-p arg))
diff --git a/test/cider-find-tests.el b/test/cider-find-tests.el
index 20af749e48..095c9de6a3 100644
--- a/test/cider-find-tests.el
+++ b/test/cider-find-tests.el
@@ -69,7 +69,15 @@
     (expect (nth 2 (spy-context-args (spy-calls-most-recent 'cider-jump-to))) 
:to-be-truthy)
     (spy-calls-reset 'cider-jump-to)
     (cider-find-keyword '-)             ; negative -> other window
-    (expect (nth 2 (spy-context-args (spy-calls-most-recent 'cider-jump-to))) 
:to-be-truthy)))
+    (expect (nth 2 (spy-context-args (spy-calls-most-recent 'cider-jump-to))) 
:to-be-truthy))
+
+  ;; Regression test: with the default config and no keyword at point, `kw' is
+  ;; nil and used to reach `string-match', crashing with a raw type error.
+  (it "signals a user-error when there is no keyword at point"
+    (spy-on 'cider-ensure-session :and-return-value t)
+    (spy-on 'cider-symbol-at-point :and-return-value nil)
+    (let ((cider-prompt-for-symbol nil))
+      (expect (cider-find-keyword nil) :to-throw 'user-error))))
 
 (describe "cider--find-keyword-loc"
   (it "finds the given keyword, discarding false positives"

Reply via email to