Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on a literal used in is form #2340

Open
1 task done
NoahTheDuke opened this issue May 31, 2024 · 3 comments
Open
1 task done

Warn on a literal used in is form #2340

NoahTheDuke opened this issue May 31, 2024 · 3 comments

Comments

@NoahTheDuke
Copy link
Contributor

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})))))
@borkdude
Copy link
Member

Anything other than a symbol (reference to something) or seq (function call) is suspicious right?

@NoahTheDuke
Copy link
Contributor Author

Or a deref or var (which might be considered lists, depending on how you implement it).

@borkdude
Copy link
Member

It reminds me of the :condition-always-true linter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Medium priority (new / enhance)
Development

No branches or pull requests

2 participants