Skip to content

Commit

Permalink
Fix #1545: recur in cond-> gives warning about recur not in tail posi…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
borkdude committed Jan 15, 2022
1 parent 808ab6b commit 90aac2c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ For a list of breaking changes, check [here](#breaking-changes).
## Unreleased

- Fix [#1537](https://github.com/clj-kondo/clj-kondo/issues/1537): stackoverflow with potemkin import vars with cyclic references
- Fix [#1545](https://github.com/clj-kondo/clj-kondo/issues/1545): `recur` in `cond->` gives warning about `recur` not in tail position.

## 2022.01.13

Expand Down
11 changes: 10 additions & 1 deletion src/clj_kondo/impl/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,15 @@
:loop-without-recur "Loop without recur.")))
analyzed))))

(defn first-callstack-elt-ignoring-macros
[callstack]
(loop [callstack callstack]
(when-let [cse (first callstack)]
(if (not (one-of (second cse)
[-> ->> some-> some->> doto cond->]))
cse
(recur (rest callstack))))))

(defn analyze-recur [ctx expr]
(let [filename (:filename ctx)
recur-arity (:recur-arity ctx)
Expand All @@ -912,7 +921,7 @@
(let [len (:len ctx)
idx (:idx ctx)
parent (-> (:callstack ctx)
second second)]
rest first-callstack-elt-ignoring-macros second)]
(when (and len idx
(not= (dec len) idx)
(not (one-of parent [if case cond if-let if-not condp])))
Expand Down
3 changes: 2 additions & 1 deletion test/clj_kondo/recur_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
(is (empty? (lint! "(loop [] (recur))" linter-config))))
(testing "recur is in tail position"
(is (empty? (lint! "(fn [] (if true (recur) 3))" linter-config)))
(is (empty? (lint! "(fn [x] (case x 1 (recur (inc x)) 2 :the-end))" linter-config)))))
(is (empty? (lint! "(fn [x] (case x 1 (recur (inc x)) 2 :the-end))" linter-config)))
(is (empty? (lint! "(loop [x 10] (cond-> (dec x) (pos? x) (recur)))" linter-config)))))

0 comments on commit 90aac2c

Please sign in to comment.