Skip to content

Commit

Permalink
Arity warnings for def
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Feb 16, 2023
1 parent 41243e2 commit de025e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ For a list of breaking changes, check [here](#breaking-changes).
## Unreleased

- [#1976](https://github.com/clj-kondo/clj-kondo/issues/1976): warn about using multiple bindings after varargs (`&`) symbol in fn syntax
- Add arity checks for code `def`
- [#1971](https://github.com/clj-kondo/clj-kondo/issues/1971): false positive `:redundant-fn-wrapper` with syntax-quoted body
- [#1984](https://github.com/clj-kondo/clj-kondo/issues/1984): lint java constructor calls as unresolved-symbol when using dot notation.
- [#1970](https://github.com/clj-kondo/clj-kondo/issues/1970): `:dynamic-var-not-earmuffed` should be opt-in
Expand Down
7 changes: 4 additions & 3 deletions src/clj_kondo/impl/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1093,12 +1093,14 @@
children (next children)
docstring (when (> (count children) 1)
(string-from-token (first children)))

defmulti? (or (= 'clojure.core/defmulti defined-by)
(= 'cljs.core/defmulti defined-by))
doc-node (when docstring
(first children))
[child & children] (if docstring (next children) children)
core-def? (one-of (first (:callstack ctx)) [[clojure.core def] [cljs.core def]])
_ (when (and core-def? children)
(findings/reg-finding! ctx (utils/node->line (:filename ctx) expr :invalid-arity "Too many arguments to def")))
[extra-meta extra-meta-node children] (if (and defmulti?
child
(identical? :map (utils/tag child)))
Expand All @@ -1118,8 +1120,7 @@
children (if (:analyze-var-defs-shallowly? ctx)
[]
children)
def-init (when (and (or (= 'clojure.core/def defined-by)
(= 'cljs.core/def defined-by))
def-init (when (and core-def?
(= 1 (count children)))
(or (analyze-expression** ctx (first children))
;; prevent analysis of only child more than once
Expand Down
12 changes: 11 additions & 1 deletion test/clj_kondo/invalid_arity_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns clj-kondo.invalid-arity-test
(:require
[clj-kondo.test-utils :refer
[lint! assert-submaps]]
[lint! assert-submaps assert-submaps2]]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :as t :refer [deftest is testing]]
Expand Down Expand Up @@ -181,3 +181,13 @@
'({:file "corpus/def_fn.clj", :row 12, :col 1, :level :error,
:message "def-fn/cons is called with 3 args but expects 2"})
(lint! (io/file "corpus/def_fn.clj"))))

(deftest def-test
(assert-submaps2
[{:row 1,
:col 1,
:level :error,
:message "Too many arguments to def"}]
(lint! "(def x 1 2)"))
(is (empty? (lint! "(def x)")))
(is (empty? (lint! "(def x \"foo\" 1)"))))

0 comments on commit de025e2

Please sign in to comment.