branch: elpa/flycheck
commit 956b836e11e66db7ac21a8626927114382ba152d
Author: Peter Oliver <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    [#2404] Say why the Emacs Lisp checkers won't run in a buffer
    
    On Emacs 30+ the byte-compiling checker only runs in a buffer that
    trusted-content covers, which leaves out every file it doesn't list and
    the *scratch* buffer Emacs starts with.  flycheck-verify-setup reported
    "may enable: no" and nothing else, so people couldn't tell why their
    Emacs Lisp files went unchecked (#2402, #2404).
    
    Both checkers now verify the buffer: whether it's a file they skip on
    purpose (autoloads, package manifests, dir-locals, Eldev files) and, for
    the byte compiler, whether it's trusted, pointing at trusted-content or
    at the buffer having no file, which no list of trusted files can cover.
---
 CHANGELOG.md                            |  1 +
 flycheck.el                             | 92 ++++++++++++++++++++++++---------
 test/specs/languages/test-emacs-lisp.el | 72 ++++++++++++++++++++++++--
 3 files changed, 137 insertions(+), 28 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9a99bb17a3..28ef27892b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@
 
 ### New Features
 
+- [#2363](https://github.com/flycheck/flycheck/pull/2363): 
`flycheck-verify-setup` says why `emacs-lisp` or `emacs-lisp-checkdoc` won't 
run in a buffer, such as a file `trusted-content` doesn't cover, or a buffer 
with no file at all.
 - [#2386](https://github.com/flycheck/flycheck/pull/2386): Fixes apply to 
errors in other files of the project: `x` in the error list opens the file an 
error belongs to and applies the fix there, and `X` in the project scope fixes 
every file the list shows.
 - [#2385](https://github.com/flycheck/flycheck/pull/2385): The native 
`flycheck-lsp` client checks a file on a remote host, starting the language 
server on that host over TRAMP instead of declining the buffer.
 - [#2381](https://github.com/flycheck/flycheck/pull/2381): New commands 
`flycheck-lsp-list-servers`, `flycheck-lsp-restart-server` and 
`flycheck-lsp-shutdown-servers` show the language servers Flycheck is running 
and stop them, which previously took restarting Emacs.
diff --git a/flycheck.el b/flycheck.el
index d26bceaf68..aed7726d00 100644
--- a/flycheck.el
+++ b/flycheck.el
@@ -15904,22 +15904,30 @@ This variable has no effect, if
 
 (defun flycheck--emacs-lisp-enabled-p ()
   "Check whether to enable Emacs Lisp checker in the current buffer."
-  (not
-   (or
-    ;; Do not check buffers used for autoloads generation during package
-    ;; installation.  These buffers are too short-lived for being checked, and
-    ;; doing so causes spurious errors.  See
-    ;; https://github.com/flycheck/flycheck/issues/45 and
-    ;; https://github.com/bbatsov/prelude/issues/248.  We must also not check
-    ;; compilation buffers, but as these are ephemeral, Flycheck won't check
-    ;; them anyway.
-    (flycheck-autoloads-file-p)
-    ;; Package-manager manifests and dir-locals files contain data, not
-    ;; code, and don't need to follow Checkdoc conventions either.
-    (and (buffer-file-name)
+  (not (flycheck--emacs-lisp-disabled-reason)))
+
+(defun flycheck--emacs-lisp-disabled-reason ()
+  "Return why neither Emacs Lisp checker runs in the current buffer, or nil."
+  (cond
+   ;; Do not check buffers used for autoloads generation during package
+   ;; installation.  These buffers are too short-lived for being checked, and
+   ;; doing so causes spurious errors.  See
+   ;; https://github.com/flycheck/flycheck/issues/45 and
+   ;; https://github.com/bbatsov/prelude/issues/248.  We must also not check
+   ;; compilation buffers, but as these are ephemeral, Flycheck won't check
+   ;; them anyway.
+   ((flycheck-autoloads-file-p)
+    "autoloads file")
+   ;; Package-manager manifests and dir-locals files contain data, not
+   ;; code, and don't need to follow Checkdoc conventions either.
+   ((and (buffer-file-name)
          (member (file-name-nondirectory (buffer-file-name))
-                 '("Cask" "Carton" "Eask" "Eask-local"
-                   ".dir-locals.el" ".dir-locals-2.el"))))))
+                 '("Cask" "Carton" "Eask" "Eask-local")))
+    "package manifest")
+   ((and (buffer-file-name)
+         (member (file-name-nondirectory (buffer-file-name))
+                 '(".dir-locals.el" ".dir-locals-2.el")))
+    "dir-locals file")))
 
 (defun flycheck--emacs-lisp-byte-compile-enabled-p ()
   "Check whether to enable the Emacs Lisp byte compiler checker.
@@ -15932,16 +15940,41 @@ mark files or directories as trusted.
 Checkdoc doesn't expand macros, so `emacs-lisp-checkdoc' stays
 enabled for untrusted files, like in Emacs core."
   (and (flycheck--emacs-lisp-enabled-p)
-       (or (not (fboundp 'trusted-content-p))
-           (trusted-content-p))))
+       (not (flycheck--emacs-lisp-untrusted-reason))))
+
+(defun flycheck--emacs-lisp-untrusted-reason ()
+  "Return why the current buffer isn't trusted to byte-compile, or nil.
+
+Return nil as well where there is no notion of trust, that is, without
+`trusted-content-p'."
+  (when (and (fboundp 'trusted-content-p) (not (trusted-content-p)))
+    (if buffer-file-truename
+        "see `trusted-content'"
+      ;; Only a `trusted-content' of `:all' trusts a buffer without a file
+      "buffer visits no file")))
 
 (defun flycheck--emacs-lisp-checkdoc-enabled-p ()
   "Check whether to enable Emacs Lisp Checkdoc in the current buffer."
-  (and (flycheck--emacs-lisp-enabled-p)
-       ;; These files are valid Lisp, but don't contain "standard" comments.
-       (not (and (buffer-file-name)
-                 (member (file-name-nondirectory (buffer-file-name))
-                         '("Eldev" "Eldev-local"))))))
+  (not (flycheck--emacs-lisp-checkdoc-disabled-reason)))
+
+(defun flycheck--emacs-lisp-checkdoc-disabled-reason ()
+  "Return why Checkdoc skips the current buffer, or nil."
+  (or (flycheck--emacs-lisp-disabled-reason)
+      ;; These files are valid Lisp, but don't contain "standard" comments.
+      (and (buffer-file-name)
+           (member (file-name-nondirectory (buffer-file-name))
+                   '("Eldev" "Eldev-local"))
+           "Eldev file")))
+
+(defun flycheck--emacs-lisp-verification (label reason)
+  "Return a verification result LABEL for an Emacs Lisp checker.
+
+REASON is why the checker skips the current buffer, or nil if it
+doesn't."
+  (flycheck-verification-result-new
+   :label label
+   :message (if reason (format "no, %s" reason) "yes")
+   :face (if reason '(bold warning) 'success)))
 
 (flycheck-define-checker emacs-lisp
   "An Emacs Lisp syntax checker using the Emacs Lisp Byte compiler.
@@ -16011,6 +16044,15 @@ See Info Node `(elisp)Byte Compilation'."
       (flycheck-sanitize-errors errors))))
   :modes (emacs-lisp-mode lisp-interaction-mode)
   :enabled flycheck--emacs-lisp-byte-compile-enabled-p
+  :verify
+  (lambda (_)
+    (cons
+     (flycheck--emacs-lisp-verification
+      "checkable content" (flycheck--emacs-lisp-disabled-reason))
+     ;; Without `trusted-content-p' trust isn't a factor
+     (when (fboundp 'trusted-content-p)
+       (list (flycheck--emacs-lisp-verification
+              "trusted content" (flycheck--emacs-lisp-untrusted-reason))))))
   :predicate
   (lambda ()
     ;; Do not check buffers that should not be byte-compiled.  The checker
@@ -16095,7 +16137,11 @@ The checker runs `checkdoc-current-buffer'."
   :error-patterns
   ((info line-start (or "#<buffer  *temp*>" (file-name)) ":" line ": " 
(message) line-end))
   :modes (emacs-lisp-mode)
-  :enabled flycheck--emacs-lisp-checkdoc-enabled-p)
+  :enabled flycheck--emacs-lisp-checkdoc-enabled-p
+  :verify
+  (lambda (_)
+    (list (flycheck--emacs-lisp-verification
+           "checkable content" 
(flycheck--emacs-lisp-checkdoc-disabled-reason)))))
 
 (dolist (checker '(emacs-lisp emacs-lisp-checkdoc))
   (setf (car (flycheck-checker-get checker 'command))
diff --git a/test/specs/languages/test-emacs-lisp.el 
b/test/specs/languages/test-emacs-lisp.el
index 8db7001b35..965f9c474a 100644
--- a/test/specs/languages/test-emacs-lisp.el
+++ b/test/specs/languages/test-emacs-lisp.el
@@ -38,6 +38,14 @@
   "Check if VAR is a customization pair describing a variable."
   (eq (cadr var) 'custom-variable))
 
+(defun flycheck/verify-row (checker label)
+  "Return the message and face of CHECKER's verification row LABEL, or nil."
+  (let ((row (seq-find (lambda (r)
+                         (equal (flycheck-verification-result-label r) label))
+                       (flycheck-verify-generic-checker checker))))
+    (and row (list (flycheck-verification-result-message row)
+                   (flycheck-verification-result-face row)))))
+
 (describe "Language Emacs Lisp"
 
   (describe "Checkdoc"
@@ -66,15 +74,40 @@
         (let ((trusted-content nil))
           (expect (flycheck-may-use-checker 'emacs-lisp)
                   :not :to-be-truthy)
+          (expect (flycheck/verify-row 'emacs-lisp "trusted content")
+                  :to-equal '("no, see `trusted-content'" (bold warning)))
           ;; Checkdoc doesn't expand macros, so it stays enabled
           (expect (flycheck-may-use-checker 'emacs-lisp-checkdoc)
-                  :to-be-truthy))))
+                  :to-be-truthy)
+          (expect (flycheck/verify-row 'emacs-lisp-checkdoc "trusted content")
+                  :to-be nil))))
 
     (it "enables byte compilation for trusted files"
       (assume (fboundp 'trusted-content-p) "Requires Emacs 30+")
       (flycheck-buttercup-with-resource-buffer 
"language/emacs-lisp/warnings.el"
         (emacs-lisp-mode)
-        (expect (flycheck-may-use-checker 'emacs-lisp) :to-be-truthy))))
+        (expect (flycheck-may-use-checker 'emacs-lisp) :to-be-truthy)
+        (expect (flycheck/verify-row 'emacs-lisp "trusted content")
+                :to-equal '("yes" success))))
+
+    (it "says a buffer without a file can't be trusted"
+      (assume (fboundp 'trusted-content-p) "Requires Emacs 30+")
+      ;; Like the *scratch* buffer Emacs starts with
+      (flycheck-buttercup-with-temp-buffer
+        (lisp-interaction-mode)
+        (let ((trusted-content nil))
+          (expect (flycheck-may-use-checker 'emacs-lisp) :not :to-be-truthy)
+          (expect (flycheck/verify-row 'emacs-lisp "trusted content")
+                  :to-equal '("no, buffer visits no file" (bold warning))))))
+
+    (it "does not verify trust without trusted-content-p"
+      (assume (not (fboundp 'trusted-content-p)) "Trust is a factor here")
+      (flycheck-buttercup-with-resource-buffer 
"language/emacs-lisp/warnings.el"
+        (emacs-lisp-mode)
+        (expect (flycheck/verify-row 'emacs-lisp "checkable content")
+                :to-equal '("yes" success))
+        (expect (flycheck/verify-row 'emacs-lisp "trusted content")
+                :to-be nil))))
 
   (describe "Checker tests"
     (flycheck-buttercup-def-checker-test (emacs-lisp emacs-lisp-checkdoc) 
emacs-lisp nil
@@ -159,21 +192,50 @@
       ;; Regression test ensuring that Emacs Lisp won't check autoload buffers.
       (flycheck-buttercup-with-file-buffer (locate-library "shut-up-autoloads")
         (expect (flycheck-may-use-checker 'emacs-lisp) :not :to-be-truthy)
-        (expect (flycheck-may-use-checker 'emacs-lisp-checkdoc) :not 
:to-be-truthy)))
+        (expect (flycheck/verify-row 'emacs-lisp "checkable content")
+                :to-equal '("no, autoloads file" (bold warning)))
+        (expect (flycheck-may-use-checker 'emacs-lisp-checkdoc) :not 
:to-be-truthy)
+        (expect (flycheck/verify-row 'emacs-lisp-checkdoc "checkable content")
+                :to-equal '("no, autoloads file" (bold warning)))))
 
     (flycheck-buttercup-def-checker-test (emacs-lisp emacs-lisp-checkdoc) 
emacs-lisp
                                          checkdoc-does-not-check-manifests
       ;; Flycheck's own manifest, which is real Lisp but is data
       (flycheck-buttercup-with-file-buffer
           (expand-file-name "Eask" flycheck-test-source-directory)
-        (expect (flycheck-may-use-checker 'emacs-lisp-checkdoc) :not 
:to-be-truthy)))
+        ;; It would otherwise open in `eask-mode', or `fundamental-mode'
+        ;; without that, which no Emacs Lisp checker supports anyway
+        (emacs-lisp-mode)
+        (expect (flycheck-may-use-checker 'emacs-lisp-checkdoc) :not 
:to-be-truthy)
+        (expect (flycheck/verify-row 'emacs-lisp-checkdoc "checkable content")
+                :to-equal '("no, package manifest" (bold warning)))))
 
     (it "does not checkdoc an Eldev file"
       ;; The name was matched against the whole path, so it never matched
       (flycheck-buttercup-with-temp-buffer
         (setq buffer-file-name (expand-file-name "Eldev" default-directory))
         (emacs-lisp-mode)
-        (expect (flycheck--emacs-lisp-checkdoc-enabled-p) :not :to-be-truthy)))
+        (expect (flycheck--emacs-lisp-checkdoc-enabled-p) :not :to-be-truthy)
+        (expect (flycheck/verify-row 'emacs-lisp-checkdoc "checkable content")
+                :to-equal '("no, Eldev file" (bold warning)))))
+
+    (it "does not check a dir-locals file"
+      (flycheck-buttercup-with-temp-buffer
+        (setq buffer-file-name (expand-file-name ".dir-locals.el" 
default-directory))
+        (emacs-lisp-mode)
+        (expect (flycheck--emacs-lisp-enabled-p) :not :to-be-truthy)
+        (expect (flycheck/verify-row 'emacs-lisp "checkable content")
+                :to-equal '("no, dir-locals file" (bold warning)))
+        (expect (flycheck/verify-row 'emacs-lisp-checkdoc "checkable content")
+                :to-equal '("no, dir-locals file" (bold warning)))))
+
+    (it "verifies an ordinary file as checkable"
+      (flycheck-buttercup-with-resource-buffer 
"language/emacs-lisp/warnings.el"
+        (emacs-lisp-mode)
+        (expect (flycheck/verify-row 'emacs-lisp "checkable content")
+                :to-equal '("yes" success))
+        (expect (flycheck/verify-row 'emacs-lisp-checkdoc "checkable content")
+                :to-equal '("yes" success))))
 
     (flycheck-buttercup-def-checker-test (emacs-lisp emacs-lisp-checkdoc) 
emacs-lisp
                                          does-not-check-with-no-byte-compile

Reply via email to