Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

common lisp - SBCL Compiler Diagnostic Messages (missing with a "when" with no body)

By accident, I recently came across a latent coding error in one of my functions, dealing with a when statement. A reduced paraphrase might look like:

(defparameter a 0)

(when (or (= a 0)
          (= a 1)
  (* a a)))

The problem is a misplaced parenthesis, so it actually is

(when (or (= a 0)
          (= a 1)
          (* a a)))

In a situation like this, wouldn't it be useful for the compiler to generate either a style warning or note? It seems to me that the meaning of a when statement normally implies a condition and a body, even though the body is strictly optional. Of course, a print pretty would have caught this in the editor, but I had copied it from elsewhere. Is there a reason that SBCL does not check for these kinds of mistakes?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

a print pretty would have caught this in the editor

To discuss the options, I?know about:

  • trivial-formatter will format the source code.

    (trivial-formatter:fmt :your-system :supersede)

  • cl-indentify indents the source code. Has a command line utility. I tried it once and it was not bad, but different than Emacs' indentation, thus annoying for me.

    $ cl-indentify bar.lisp

It links to lispindent but I was less happy with its result.

However, the best would be to not only format the code and re-read it ourselves, but to

run checks against a set of rules to warn against code smells

This is what proposes the lisp-critic. It can critique a function or a file. However:

  • (edit) it doesn't really have a Slime integration, we have to either critique a function or a whole file.
    • if you feel adventurous, see an utility of mine here. It could be an easier way to test snippets that you enter at the REPL.
  • it hasn't the rule about when without a body (we can easily add it)

And it would be best that the run failed with an error status code if it found a code smell. Again, a little project of mine in beta tries to do that, see here. It doesn't have much rules now, but I just pushed a check for this. You can call the script:

$colisper.sh tests/playground.lisp 

it shows an error (but doesn't write it in-place by default):

 |;; when with no body
 |(when (or (= a 0)
 |          (= a 1)
!|     (* a a))
!|  (error "colisper found a 'when' with a missing body. (we should error the script without this rewrite!)"))

and returns with an exit code, so we can use it has a git hook or on a CI pipeline.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...