Flycheck has a brilliant ability to warn about when I'm using a function foo-bar but have forgotten to add (require 'foo) at the top of my file.

However, it's inconsistent. It doesn't always warn for functions from some packages, even if the function is not autoloaded. How can I make Emacs warn me about these?

;; This produces a warning
;; "the function `which-function' is not known to be defined."
(defun which-func-not-required ()

;; This produces no warning.
(defun cl-not-required (x)
  (cl-copy-list x))

;; This produces no warning either.
(defun byte-comp-not-required (f)
  (byte-compile-top-level f))

;; end

(inspired by this PR)


You will never get warnings about functions from bytecomp.el being undefined during byte-compilation, for a very obvious reason:

bytecomp.el implements the byte compiler, so it is naturally loaded and all functions therein defined during byte-compilation, whether the compiled files loads it or not. This also affects all libraries loaded by bytecomp.el, notably macroexp.

There is nothing that you or Flycheck (or, for that matter, any Emacs Lisp code) could do about this. It is a fundamental flaw in Emacs: Libraries are compiled within the running Emacs session, and thus inherit the entire environment (including all definitions) of this session.

You should get warnings about undefined functions from cl-lib, though. If you don't, you do not compile in a clean session. Check your settings.

  • "Check your settings" aha, turns out my (setq-default flycheck-emacs-lisp-initialize-packages t) caused this behaviour. I'll look at cask instead. – Wilfred Hughes Oct 26 '14 at 18:57
  • "There is nothing [..] any Emacs Lisp code could do about this" the byte compiler is just elisp, I suppose one could write a standalone, pure elisp lint and run it in a clean environment. It'd be a huge task though. – Wilfred Hughes Oct 26 '14 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.