Clj-kondo and a request handler

Hi guys!

clj-kondo is complaining about an “unused binding req” in my Ring handler function (I’m using org.httpkit.server). From what I read here: https://github.com/ring-clojure/ring/wiki/Concepts I have to have at least one argument, a map representing a request - which makes sense to me, even if I don’t use that map in my function. I imagine req is in fact used somehow, only in a different namespace that the linter can’t see?

Should I just put a silly (comment req) Or what is the best practice here?

Snippet of my code, not that it shows much:

(defn app [req] 
  {:status  200
   :headers {"Content-Type" "text/html"}
   :body    (tv-dash!)})

(defn -main
  "I don't do a whole lot} ... yet."
  [& args]
  (run-server app {:port 9090}))

You can start the argument name with an underscore to indicate that it’s unused. Then clj-kondo will not warn about it anymore.

1 Like

Hi @duzunov, welcome to the community.

My guess is that clj-kondo is complaining that req is never used, so you could change your handler to be:

(defn app [_]
  {:status 200
   :headers {“Content-Type” “text/html”}
   :body (tv-dash!)})

In Clojure it is common to use _ as a name for vars when you don’t intend to use them but they are required by an “interface” like request handlers, or when a function returns soething that you might not care about.

HTH

1 Like

Whoa, the Clojure community is incredible! I never expected you guys to answer my silly question so quickly.

Thanks @borkdude and @kennethkalmer!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.