Skip to content

Commit

Permalink
[#3] suggest seq over (not (empty? ...))
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jul 7, 2019
1 parent 2e48e04 commit 9075043
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/clj_kondo/impl/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@
(analyze-children (ctx-with-linter-disabled ctx :unresolved-symbol)
(next (:children expr))))

(defn analyze-empty?
[ctx expr]
(let [cs (:callstack ctx)
not-expr (one-of (second cs) [[clojure.core not] [cljs.core not]])]
(when not-expr
(findings/reg-finding!
(:findings ctx)
(node->line (:filename ctx) not-expr
:warning :not-empty?
"use the idiom (seq x) rather than (not (empty? x))")))
(analyze-children ctx (:children expr))))

(defn analyze-call
[{:keys [:top-level? :base-lang :lang :ns :config] :as ctx}
{:keys [:arg-count
Expand All @@ -836,7 +848,8 @@
ctx (if fq-sym
(update ctx :callstack
(fn [cs]
(cons [resolved-namespace resolved-name] cs)))
(cons (with-meta [resolved-namespace resolved-name]
(meta expr)) cs)))
ctx)
resolved-as-clojure-var-name
(when (one-of resolved-as-namespace [clojure.core cljs.core])
Expand Down Expand Up @@ -897,6 +910,7 @@
areduce (analyze-areduce ctx expr)
this-as (analyze-this-as ctx expr)
memfn (analyze-memfn ctx expr)
empty? (analyze-empty? ctx expr)
;; catch-all
(case [resolved-namespace resolved-name]
[schema.core defn]
Expand Down
3 changes: 2 additions & 1 deletion src/clj_kondo/impl/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
(clojure.test/are)
(clojure.test/is [thrown-with-msg?])
]}
:misplaced-docstring {:level :warning}}
:misplaced-docstring {:level :warning}
:not-empty? {:level :warning}}
:lint-as {cats.core/->= clojure.core/->
cats.core/->>= clojure.core/->>
rewrite-clj.custom-zipper.core/defn-switchable clojure.core/defn
Expand Down
9 changes: 9 additions & 0 deletions test/clj_kondo/main_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,15 @@
:message "invalid function body"})
(lint! "(defn f \"dude\" x) (f 1)")))

(deftest not-empty?-test
(assert-submaps
'({:file "<stdin>",
:row 1,
:col 1,
:level :warning,
:message "use the idiom (seq x) rather than (not (empty? x))"})
(lint! "(not (empty? [1]))")))

;;;; Scratch

(comment
Expand Down

0 comments on commit 9075043

Please sign in to comment.