为 flycheck elisp 模式添加忽略特定行功能

在 elisp 模式下打开 flycheck 时,flycheck 使用 Emacs 自身的 byte compile 来进行错误检查。 大多数时候工作得不错,偶尔需要忽略特定行的错误提示时 (类似Python 代码中的 #noqa )可以用如下方法 :

  1. 在 .emacs 文件中(use-package flycheck的config部分) 添加:
  (defcustom flycheck-elisp-noflycheck-marker ";noflycheck"
    "Flycheck line regions marked with this marker string are ignored."
    :type 'string
    :group 'flycheck)

  (defun flycheck-elisp-noflycheck (err)
    "Ignore flycheck if line of ERR contain value of  `flycheck-elisp-noflycheck-marker'."
    (save-excursion
      ;;(debug)
      (goto-char (cdr (flycheck-error-region-for-mode err 'symbols)))  
      (let ((text (buffer-substring (line-beginning-position) (line-end-position))))
        (when (string-match-p flycheck-elisp-noflycheck-marker text) ;noflycheck
          (setq flycheck-current-errors (delete  err flycheck-current-errors )) 
          t
          ))
      )
    )

  (defun elisp-noflycheck-hook ()
    "Add the ;;;###noflycheck thing to elisp."
    (require 'flycheck)
    (add-hook 'flycheck-process-error-functions #'flycheck-elisp-noflycheck nil t)) 
  
  (add-hook 'emacs-lisp-mode-hook #'elisp-noflycheck-hook) ;noflycheck

  1. 指定行结尾添加 ;noflycheck 注释

原始方案出自 : https://emacs.stackexchange.com/questions/47878/how-can-i-disable-a-specific-lint-error-for-emacs-lisp-using-flycheck