Skip to content

Warn on a literal used in is form #2340

@NoahTheDuke

Description

@NoahTheDuke

To upvote this issue, give it a thumbs up. See this list for the most upvoted issues.

  • I have read the Clojure etiquette and will respect it when communicating on this platform.

Is your feature request related to a problem? Please describe.
When editing tests, it can be easy to accidentally remove or not include the comparison function in the given form, leaving a literal in the form position. This is very easy when you don't write a msg so the two values aren't compared but used as form and msg:

(deftest example-test
  (is #{} (set (some-func a b c)))

instead of:

(deftest example-test
  (is (= #{} (set (some-func a b c))))

Describe the solution you'd like
A simple check that the first element to clojure.test/is is a list or symbol, aka not a data "literal".

Describe alternatives you've considered
Do nothing?

Additional context
I have a working hook for this, idk how easy it is to port that to clj-kondo itself.

(defn literal?
  [node]
  (or (#{:map :set :string :vector :regex} (api/tag node))
      (api/keyword-node? node)
      (and (api/token-node? node) (number? (:value node)))))

(defn -is
  [{:keys [node]}]
  (let [[_is form] (:children node)]
    (when (literal? form)
      (let [{:keys [row col end-row end-col]} (meta form)]
        (api/reg-finding! {:message "`is` asserted a literal."
                           :type :muncher/is-literal
                           :row row
                           :col col
                           :end-row end-row
                           :end-col end-col})))))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Medium priority (new / enhance)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions