From 6d307e5f8f2e07b6306002488f5ceaab494f9ed2 Mon Sep 17 00:00:00 2001 From: brotzeit Date: Fri, 17 Dec 2021 12:51:33 +0100 Subject: [PATCH] automatically detect toolchain and use correct clippy params close #171 --- README.md | 15 ++++++--------- rustic-flycheck.el | 22 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8f315cc3..eec7ccd4 100644 --- a/README.md +++ b/README.md @@ -483,7 +483,7 @@ it with `rustup component add --toolchain nightly clippy`. ### Flycheck -In case you want to see clippy lints with flycheck, you can activate +In case you want to use clippy with flycheck but without LSP, you can activate this checker and use the command `flycheck-list-errors` ```elisp @@ -496,15 +496,12 @@ Turn off flycheck. (remove-hook 'rustic-mode-hook 'flycheck-mode) ``` -The parameters of the checker can be modified with `rustic-flycheck-clippy-params` -and are by default configured for using unstable options that are only available -on the nightly toolchains. +The checker automatically detects the active toolchain and applies the +correct parameters You can set a default value for both stable and +nightly toolchains. These are the default values. -If you are using the stable toolchain you have to change the value: - -```elisp -(setq rustic-flycheck-clippy-params "--message-format=json") -``` +- `rustic-flycheck-clippy-params-stable` "--message-format=json" +- `rustic-flycheck-clippy-params-nightly` "--message-format=json -Zunstable-options" ### lsp-mode diff --git a/rustic-flycheck.el b/rustic-flycheck.el index 0bf566a4..083525c7 100644 --- a/rustic-flycheck.el +++ b/rustic-flycheck.el @@ -9,8 +9,13 @@ (require 'rustic) -(defcustom rustic-flycheck-clippy-params "--message-format=json -Zunstable-options" - "Parameters for the flycheck clippy checker `rustic-clippy'." +(defcustom rustic-flycheck-clippy-params-stable "--message-format=json" + "Parameters for the flycheck clippy checker `rustic-clippy' when active toolchain is stable." + :type 'string + :group 'rustic-flycheck) + +(defcustom rustic-flycheck-clippy-params-nightly "--message-format=json -Zunstable-options" + "Parameters for the flycheck clippy checker `rustic-clippy' when active toolchain is nightly." :type 'string :group 'rustic-flycheck) @@ -160,11 +165,22 @@ Flycheck according to the Cargo project layout." (setq-local flycheck-rust-crate-type .kind) (setq-local flycheck-rust-binary-name .name))))) +(defun rustic-flycheck-nightly-p () + "Check if active toolchain is a nightly toolchain." + (let ((tc (shell-command-to-string "rustup show active-toolchain"))) + (string-match-p "^nightly" (car (split-string tc))))) + +(defun rustic-flycheck-clippy-params () + "Return clippy parameters for flycheck depending on the active toolchain." + (if (rustic-flycheck-nightly-p) + rustic-flycheck-clippy-params-nightly + rustic-flycheck-clippy-params-stable)) + (flycheck-define-checker rustic-clippy "A Rust syntax checker using clippy. See URL `https://github.com/rust-lang-nursery/rust-clippy'." - :command ("cargo" "clippy" (eval (split-string rustic-flycheck-clippy-params))) + :command ("cargo" "clippy" (eval (split-string (rustic-flycheck-clippy-params)))) :error-parser flycheck-parse-cargo-rustc :error-filter flycheck-rust-error-filter :error-explainer flycheck-rust-error-explainer