Skip to content

Commit

Permalink
Check for even number of clauses for cond-> and cond->>
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdl89 committed Nov 5, 2024
1 parent 166be29 commit c3d4cb8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ For a list of breaking changes, check [here](#breaking-changes).
- [#2410](https://github.com/clj-kondo/clj-kondo/issues/2410): add `--report-level` flag
- [#2416](https://github.com/clj-kondo/clj-kondo/issues/2416): detect empty `require` and `:require` forms ([@NoahTheDuke](https://github.com/NoahTheDuke))
- [#1786](https://github.com/clj-kondo/clj-kondo/issues/1786): Support `gen-interface` (by suppressing unresolved symbols)
- [#2420](https://github.com/clj-kondo/clj-kondo/issues/2420): Detect uneven number of clauses in cond-> and cond->>

## 2024.09.27

Expand Down
16 changes: 13 additions & 3 deletions src/clj_kondo/impl/macroexpand.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns clj-kondo.impl.macroexpand
{:no-doc true}
(:require
[clj-kondo.impl.utils :refer [parse-string tag vector-node list-node token-node]]
[clj-kondo.impl.findings :as findings]
[clj-kondo.impl.utils :refer [parse-string tag vector-node list-node
token-node node->line]]
[clojure.walk :as walk]))

(set! *warn-on-reflection* true)
Expand Down Expand Up @@ -54,7 +56,7 @@

(defn expand-cond->
"Expands cond-> and cond->>"
[_ctx expr resolved-as-name]
[ctx expr resolved-as-name]
(let [[_ start-expr & clauses] (:children expr)
thread-sym (case resolved-as-name
cond-> 'clojure.core/->
Expand All @@ -74,7 +76,15 @@
(vector-node
(list* g start-expr
(interleave (repeat g) (butlast steps))))
(if (empty? steps) g (last steps))])]
(if (empty? steps) g (last steps))])
clauses-count (count clauses)]
(when (odd? clauses-count)
(findings/reg-finding! ctx
(node->line
(:filename ctx)
expr
:invalid-arity
(str resolved-as-name " requires even number of clauses"))))
ret))

(defn expand-doto [_ctx expr]
Expand Down
14 changes: 14 additions & 0 deletions test/clj_kondo/invalid_arity_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,17 @@
:level :error,
:message "clojure.core/throw is called with 2 args but expects 1"}]
(lint! "(throw 1 2)")))

(deftest cond-thread-test
(assert-submaps2
[{:row 1,
:col 1,
:level :error,
:message "cond-> requires even number of clauses"}]
(lint! "(cond-> 42 inc)"))
(assert-submaps2
[{:row 1,
:col 1,
:level :error,
:message "cond->> requires even number of clauses"}]
(lint! "(cond->> 42 (even? 7) inc (odd? 8))")))

0 comments on commit c3d4cb8

Please sign in to comment.