Skip to content

Commit

Permalink
Merge pull request #342 from brotzeit/flycheck-check-toolchain
Browse files Browse the repository at this point in the history
automatically detect toolchain and use correct clippy params
  • Loading branch information
brotzeit authored Dec 17, 2021
2 parents b142a57 + 6d307e5 commit fb6f6e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
22 changes: 19 additions & 3 deletions rustic-flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fb6f6e8

Please sign in to comment.