Liking cljdoc? Tell your friends :D

Lint

lint/assoc-fn

Enabled by defaultVersion AddedVersion Updated
true0.10.1

assoc-ing an update with the same key is hard to read. update is known and idiomatic.

Examples

; bad
(assoc coll :a (+ (:a coll) 5))
(assoc coll :a (+ (coll :a) 5))
(assoc coll :a (+ (get coll :a) 5))

; good
(update coll :a + 5)

lint/body-unquote-splicing

Enabled by defaultVersion AddedVersion Updated
true1.01.0

A macro that nests an unquote-splicing in a macro with a & body can lead to subtle hard to debug errors. Better to wrap the unquote-splicing in a do to force it into 'expression position'.

Examples

; bad
`(binding [max mymax] ~@body)

; good
`(binding [max mymax] (let [res# (do ~@body)] res#))

Reference


lint/divide-by-one

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Checks for (/ x 1).

Examples

; bad
(/ x 1)

; good
x

lint/dorun-map

Enabled by defaultVersion AddedVersion Updated
true0.11.2.1

map is lazy, which carries a performance and memory cost. dorun uses seq iteration to realize the entire sequence, returning nil. This style of iteration also carries a performance and memory cost. dorun is intended for more complex sequences, whereas a simple map can be accomplished with reduce + conj.

run! uses reduce which non-lazy and has no seq overhead.

Examples

; bad
(dorun (map println (range 10)))

; good
(run! println (range 10))

lint/dot-class-method

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Using the Obj/staticMethod form maps the method call to Clojure's natural function position.

Examples

; bad
(. Obj staticMethod args)
(. Obj (staticMethod) args)

; good
(Obj/staticMethod args)

lint/dot-obj-method

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Using the .method form maps the method call to Clojure's natural function position.

Examples

; bad
(. obj method args)

; good
(.method obj args)

lint/duplicate-field-name

Enabled by defaultVersion AddedVersion Updated
true0.1.1190.1.119

deftype and defrecord will throw errors if you define multiple fields with the same name, but it's good to catch these things early too.

Examples

; bad
(defrecord Foo [a b a])

; good
(defrecord Foo [a b c])

Reference


lint/fn-wrapper

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Avoid wrapping functions in pass-through anonymous function defitions.

Examples

; bad
(fn [num] (even? num))

; good
even?

; bad
(let [f (fn [num] (even? num))] ...)

; good
(let [f even?] ...)

Reference


lint/if-else-nil

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Idiomatic if defines both branches. when returns nil in the else branch.

Examples

; bad
(if (some-func) :a nil)

; good
(when (some-func) :a)

Reference


lint/if-let-else-nil

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Idiomatic if-let defines both branches. when-let returns nil in the else branch.

Examples

; bad
(if-let [a 1] a nil)

; good
(when-let [a 1] a)

lint/if-nil-else

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Idiomatic if defines both branches. when-not returns nil in the truthy branch.

Examples

; bad
(if (some-func) nil :a)

; good
(when-not (some-func) :a)

lint/if-not-both

Enabled by defaultVersion AddedVersion Updated
true0.10.1

if-not exists, so use it.

Examples

; bad
(if (not x) y z)

; good
(if-not x y z)

Reference


lint/if-not-do

Enabled by defaultVersion AddedVersion Updated
true0.10.1

when-not already defines an implicit do. Rely on it.

Examples

; bad
(if-not x (do (println :a) (println :b) :c))

; good
(when-not x (println :a) (println :b) :c)

lint/if-not-not

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Two nots cancel each other out.

Examples

; bad
(if-not (not x) y z)

; good
(if x y z)

lint/if-same-truthy

Enabled by defaultVersion AddedVersion Updated
true0.10.1

or exists so use it lol.

Examples

; bad
(if x x y)

; good
(or x y)

lint/into-literal

Enabled by defaultVersion AddedVersion Updated
true0.10.1

vec and set are succinct and meaningful.

Examples

; bad
(into [] coll)

; good
(vec coll)

; bad
(into #{} coll)

; good
(set coll)

lint/let-if

Enabled by defaultVersion AddedVersion Updated
true0.1.690.1.69

if-let exists so use it. Suggestions can be wrong as there's no code-walking to determine if result binding is used in falsy branch.

Examples

; bad
(let [result (some-func)] (if result (do-stuff result) (other-stuff)))

; good
(if-let [result (some-func)] (do-stuff result) (other-stuff))

Reference


lint/let-when

Enabled by defaultVersion AddedVersion Updated
true0.1.690.1.69

when-let exists so use it.

Examples

; bad
(let [result (some-func)] (when result (do-stuff result)))

; good
(when-let [result (some-func)] (do-stuff result))

Reference


lint/loop-do

Enabled by defaultVersion AddedVersion Updated
true0.10.1

loop has an implicit do. Use it.

Examples

; bad
(loop [] (do (println 1) (println 2)))

; good
(loop [] (println 1) (println 2))

lint/loop-empty-when

Enabled by defaultVersion AddedVersion Updated
true0.10.1

Empty loops with nested when can be while.

Examples

; bad
(loop [] (when (some-func) (println 1) (println 2) (recur)))

; good
(while (some-func) (println 1) (println 2) (recur))

lint/missing-body-in-when

Enabled by defaultVersion AddedVersion Updated
true0.1.690.1.69

when calls should have at least 1 expression after the condition.

Examples

; bad
(when true)
(when (some-func))

; good
(when true (do-stuff))
(when (some-func) (do-stuff))

lint/not-empty?

Enabled by defaultVersion AddedVersion Updated
true0.11.2.0

seq returns nil when given an empty collection. empty? is implemented as (not (seq coll)) so it's best and fastest to use seq directly.

Examples

; bad
(not (empty? coll))

; good (chosen style :seq (default))
(seq coll)

; good (chosen style :not-empty)
(not-empty coll)

Configurable Attributes

NameDefaultOptions
:chosen-style:seq:seq, :not-empty

Reference


lint/prefer-method-values

Enabled by defaultVersion AddedVersion Updated
true1.131.13

NOTE: Requires Clojure version 1.12.0.

Uniform qualified method values are a new syntax for calling into java code. They must resolve to a single static or instance method and to help with that, a new metadata syntax can be used: ^[] aka ^{:param-tags []}. Types are specified with classes, each corrosponding to an argument in the target method: (^[long String] SomeClass/someMethod 1 "Hello world!"). It compiles to a direct call without any reflection, guaranteeing optimal performance.

If it doesn't resolve to a single method, then the Clojure compiler throws a syntax error (IllegalArgumentException). Such ahead-of-time compilation checking is a powerful and helpful tool in writing correct and performant code. Given that, it is preferable to exclusively use method values.

Examples

; bad
(.toUpperCase "noah")
(. "noah" toUpperCase)

; good
(^[] String/toUpperCase "noah")

Reference


lint/prefer-require-over-use

Enabled by defaultVersion AddedVersion Updated
true1.3.01.3.0

In the ns form prefer :require :as over :require :refer over :require :refer :all. Prefer :require over :use; the latter form should be considered deprecated for new code.

Examples

; bad
(ns examples.ns
  (:use clojure.zip))

; good
(ns examples.ns
  (:require [clojure.zip :as zip]))
(ns examples.ns
  (:require [clojure.zip :refer [lefts rights]]))
(ns examples.ns
  (:require [clojure.zip :refer :all]))

Configurable Attributes

NameDefaultOptions
:chosen-style:as:as, :refer, :all

Reference


lint/redundant-call

Enabled by defaultVersion AddedVersion Updated
true0.10.1

A number of core functions take any number of arguments and return the arg if given only one. These calls are effectively no-ops, redundant, so they should be avoided.

Current list of clojure.core functions this linter checks:

  • ->, ->>
  • cond->, cond->>
  • some->, some->>
  • comp, partial, merge
  • min, max, distinct?

Examples

; bad
(-> x)
(->> x)
(cond-> x)
(cond->> x)
(some-> x)
(some->> x)
(comp x)
(partial x)
(merge x)
(min x)
(max x)
(distinct? x)

; good
x

lint/require-explicit-param-tags

Enabled by defaultVersion AddedVersion Updated
false1.131.13

NOTE: Requires Clojure version 1.12.0.

Uniform qualified method values are a new syntax for calling into java code. They must resolve to a single static or instance method and to help with that, a new metadata syntax can be used: ^[] aka ^{:param-tags []}. Types are specified with classes, each corrosponding to an argument in the target method: (^[long String] SomeClass/someMethod 1 "Hello world!")

If :param-tags is left off of a method value, then the compiler treats it as taking no arguments (a 0-arity static method or a 1-arity instance method with the instance being the first argument). And an _ can be used as a wild-card in the cases where there is only a single applicable method (no overloads).

These last two features are where there can be trouble. If, for whatever reason, the Java library adds an overload on type, then both the lack of :param-tags and a wild-card can lead to ambiguity. This is a rare occurence but risky/annoying enough that it's better to be explicit overall.

The styles are named after what they're looking for:

  • :missing checks that there exists a :param-tags on a method value.
  • :wildcard checks that there are no usages of _ in an existing :param-tags.
  • :both checks both conditions.

Examples

; bad (chosen style :both or :missing)
(java.io.File/mkdir (clojure.java.io/file "a"))

; bad (chosen style :both or :wildcard)
(^[_ _] java.io.File/createTempFile "abc" "b")

; good (chosen style :both or :missing)
(^[] java.io.File/mkdir (clojure.java.io/file "a"))

; good (chosen style :both or :wildcard (default))
(^[String String] java.io.File/createTempFile "abc" "b")

Configurable Attributes

NameDefaultOptions
:chosen-style:wildcard:both, :missing, :wildcard

Reference


lint/take-repeatedly

Enabled by defaultVersion AddedVersion Updated
true0.10.1

repeatedly has an arity for limiting the number of repeats with take.

Examples

; bad
(take 5 (repeatedly (range 10))

; good
(repeatedly 5 (range 10))

lint/thread-macro-one-arg

Enabled by defaultVersion AddedVersion Updated
true0.11.2.1

Threading macros require more effort to understand so only use them with multiple args to help with readability.

Examples

; bad
(-> x y)
(->> x y)

; good
(y x)

; bad
(-> x (y z))

; good
(y x z)

; bad
(->> x (y z))

; good
(y z x)

Configurable Attributes

NameDefaultOptions
:chosen-style:inline:inline, :avoid-collections

lint/try-splicing

Enabled by defaultVersion AddedVersion Updated
true1.01.0

A macro that wraps a splicing unquote in a try-catch or try-finally can lead to subtle hard to debug errors. Better to wrap the splicing unquote in a do to force it into 'expression position'.

Examples

; bad
`(try ~@body (finally :true))

; good
`(try (do ~@body) (finally :true))

Reference


lint/underscore-in-namespace

Enabled by defaultVersion AddedVersion Updated
true1.111.11

Due to munging rules, underscores in namespaces can confuse tools and libraries which expect that underscores in class names should be dashes in Clojure.

Examples

; bad
(ns foo_bar.baz_qux)

; good
(ns foo-bar.baz-qux)

lint/warn-on-reflection

Enabled by defaultVersion AddedVersion Updated
false1.8.01.8.0

Because we can't (or won't) check for interop, *warn-on-reflection* should be at the top of every file out of caution.

Examples

; bad
(ns foo.bar)
(defn baz [a b] (+ a b))

; good
(ns foo.bar)
(set! *warn-on-reflection* true)
(defn baz [a b] (+ a b))

Reference

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close