Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure :: lint-like hints

Tags:

clojure

I hunted a bug today that ultimately resulted from the following snippet somewhere in my code (where I was trying to filter for only "PRIMARY KEY" constraints in a list):

(filter #(= (% :constraint_type "PRIMARY KEY")) aListOfconstraints)

Instead of the correct:

(filter #(= (% :constraint_type) "PRIMARY KEY") aListOfconstraints)

I.e. the bug was the combined effect of a map taking a default argument in case the key is not found, as in:

({:a 1 :b 2} :a 0)

... and the equal functions accepting only one argument and returning true:

(= 1) ; evals to true

Is there any tool I could have used instead that would have prompted me to this sort of valid, but suspicious code? Or perhaps some best practice I am not aware of?

like image 256
Marcus Junius Brutus Avatar asked Dec 07 '22 09:12

Marcus Junius Brutus


2 Answers

You can look to kibit - tool that tries to perform static analysis of clojure code, using rules built on top of core.logic. It should be not so hard to add new rules, but this tool also have limitations - see project's description.

like image 118
Alex Ott Avatar answered Dec 08 '22 21:12

Alex Ott


The best practice you're looking for would be a unit test checking the proper behavior of your code.

A for the lint tool you could take a look at Eastwood, but in your case it would be difficult to provide a custom check, that could be used widely.

like image 22
harpun Avatar answered Dec 08 '22 21:12

harpun