Skip to content

Commit

Permalink
Fix clj-kondo#1954: new :uninitialized-var linter
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Feb 16, 2023
1 parent de025e2 commit bcde066
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ For a list of breaking changes, check [here](#breaking-changes).

- [#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`
- [#1954](https://github.com/clj-kondo/clj-kondo/issues/1954): new `:uninitialized-var` linter. See [docs]().
- [#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
12 changes: 12 additions & 0 deletions doc/linters.md
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,18 @@ You can add or override type annotations. See

*Example message:* `j is not bound in this destructuring form`.

### Uninitialized var

*Keyword:* `:uninitialized-var`

*Description:* warn on var without initial value

*Default level:* `:off` (will become `:warning` in the future)

*Example trigger:* `(def x)`

*Example message:* `Uninitialized var`

### Unused binding

*Keyword:* `:unused-binding`.
Expand Down
2 changes: 2 additions & 0 deletions src/clj_kondo/impl/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,8 @@
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")))
_ (when-not child
(findings/reg-finding! ctx (utils/node->line (:filename ctx) expr :uninitialized-var "Uninitialized var")))
[extra-meta extra-meta-node children] (if (and defmulti?
child
(identical? :map (utils/tag child)))
Expand Down
4 changes: 3 additions & 1 deletion src/clj_kondo/impl/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@
:dynamic-var-not-earmuffed {:level :off}
:earmuffed-var-not-dynamic {:level :warning}
:duplicate-field {:level :error}
:aliased-namespace-var-usage {:level :warning}}
:aliased-namespace-var-usage {:level :warning}
:uninitialized-var {;; will be :warning in a future release
:level :off}}
;; :hooks {:macroexpand ... :analyze-call ...}
:lint-as {cats.core/->= clojure.core/->
cats.core/->>= clojure.core/->>
Expand Down
13 changes: 13 additions & 0 deletions test/clj_kondo/uninitialized_var_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns clj-kondo.uninitialized-var-test
(:require
[clj-kondo.test-utils :as tu :refer [lint! assert-submaps2]]
[clojure.test :as t :refer [deftest is testing]]))

(deftest uninitialized-var-test
(assert-submaps2
[{:row 1,
:col 1,
:level :warning,
:message "Uninitialized var"}]
(lint! "(def x)" {:linters {:uninitialized-var {:level :warning}}})))

0 comments on commit bcde066

Please sign in to comment.