Clojure :: lint-like hints

Today I was looking for an error that ultimately arose from the following code fragment somewhere in my code (where I tried to filter only the PRIMARY KEY restrictions in the list):

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

Instead of the correct one:

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

those. the error was the combined effect of a card accepting a default argument if the key was not found, as in:

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

... and equal functions take only one argument and return true:

 (= 1) ; evals to true 

Is there any tool I could use instead that would prompt me to similar valid but suspicious code? Or maybe some best practice that I don't know about?

+4
source share
4 answers

You can see kibit , a tool that attempts to perform static analysis of clojure code using rules built on top of core.logic . You should not add new rules, but this tool also has limitations - see the Project Description.

+5
source

The best practice you are looking for will be unit test validation of your code.

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

+5
source

If you have such problems, it is better to use the right indent. In your case

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

And if you have

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

You can obviously notice the error.

+3
source

Eastwood, mentioned in an earlier answer, is now capable of catching your specific problem and several other species from the beginning of 2014. Check it out: https://github.com/jonase/eastwood

+2
source

All Articles