From 3b0a83a68c1ed1955344b0787fc28337b2a54623 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 8 Apr 2025 20:52:07 +0900 Subject: [PATCH] Prevent Flycheck from showing errors due to deleted temp files during editing Suppress unnecessary Flycheck errors when PHPStan reports "[ERROR] No files found to analyse." This happens because temporary files are deleted while editing, causing PHPStan to fail to find files. An around-advice on `flycheck-finish-checker-process` has been added to ignore such cases, preventing misleading errors from being shown in modified buffers. --- flycheck-phpstan.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/flycheck-phpstan.el b/flycheck-phpstan.el index a63771e..b3c60f5 100644 --- a/flycheck-phpstan.el +++ b/flycheck-phpstan.el @@ -44,6 +44,8 @@ ;; Usually it is defined dynamically by flycheck (defvar flycheck-phpstan-executable) (defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*") +(defvar flycheck-phpstan--output-filter-added nil) +(defconst flycheck-phpstan--nofiles-message (eval-when-compile (regexp-quote "[ERROR] No files found to analyse."))) (defcustom flycheck-phpstan-ignore-metadata-list nil "Set of metadata items to ignore in PHPStan messages for Flycheck." @@ -57,6 +59,24 @@ :safe #'stringp :group 'phpstan) +(defun flycheck-phpstan--suppress-no-files-error (next checker exit-status files output callback cwd) + "Suppress Flycheck errors if PHPStan reports no files in a modified buffer. + +This function is intended to be used as an :around advice for +`flycheck-finish-checker-process'. + +It prevents Flycheck from displaying an error when: +- CHECKER is `phpstan', +- the current buffer is modified, +- and OUTPUT contains the message `flycheck-phpstan--nofiles-message'. + +NEXT, EXIT-STATUS, FILES, OUTPUT, CALLBACK, and CWD are the original arguments +passed to `flycheck-finish-checker-process'." + (unless (and (eq checker 'phpstan) + (buffer-modified-p) + (string-match-p flycheck-phpstan--nofiles-message output)) + (funcall next checker exit-status files output callback cwd))) + (defun flycheck-phpstan--enabled-and-set-variable () "Return path to phpstan configure file, and set buffer execute in side effect." (let ((enabled (phpstan-enabled))) @@ -71,6 +91,10 @@ (and (stringp (car-safe phpstan-executable)) (listp (cdr-safe phpstan-executable))) (null phpstan-executable))) + (unless flycheck-phpstan--output-filter-added + (advice-add 'flycheck-finish-checker-process + :around #'flycheck-phpstan--suppress-no-files-error) + (setq flycheck-phpstan--output-filter-added t)) (setq-local flycheck-phpstan-executable (car (phpstan-get-executable-and-args))))))) (defun flycheck-phpstan-parse-output (output &optional _checker _buffer)