1

I was running into a flycheck warning about a function which may not be defined.

Since the function is a small optional dependency, I am inlining it in case the package that provides cannot be loaded.

I first tried I checking if the package failed to be required before filling in the function:

(unless (require 'selcand nil t)
  (defun selcand-select () ...))

This gave me

the function ‘selcand-select’ is not known to be defined.

Which makes sense since the compiler has no idea whether the selcand package will define the function selcand-select.

So I added an explicit fboundp check:

(unless (and (require 'selcand nil t) (fboundp 'selcand-select))
  (defun selcand-select (cands &optional prompt stringify) ...))

which still gave me the same error.

erjoalgo
  • 853
  • 1
  • 5
  • 18
  • Similar to https://emacs.stackexchange.com/questions/29853/defun-inside-let-with-lexical-binding-gives-byte-compile-warning-the-function-i – npostavs Mar 16 '19 at 21:42

1 Answers1

1

What did silence the warning was making the condition simpler for the compiler:

(require 'selcand nil t) 
(unless (fboundp 'selcand-select)
    (defun selcand-select (cands &optional prompt stringify) ...))

Interestingly the following also works:

(require 'selcand nil t) 
(unless (and (require 'selcand nil t) (fboundp 'selcand-select))
    (defun selcand-select (cands &optional prompt stringify) ...))

Earlier I had also tried wrapping the form in 'eval-when-compile:

(eval-when-compile
  (unless (require 'selcand nil t)
    (defun selcand-select () ...)))

which failed with:

the function ‘selcand-select’ might not be defined at runtime.

This also makes sense for the same reason that my first approach didn't work, and eval-when-compile is not needed here since selcand-select is not a macro.

erjoalgo
  • 853
  • 1
  • 5
  • 18