Skip to content

Instantly share code, notes, and snippets.

@little-arhat
Last active April 2, 2018 19:28
Show Gist options
  • Save little-arhat/f94580e99854da2ab6ce6577b3234295 to your computer and use it in GitHub Desktop.
Save little-arhat/f94580e99854da2ab6ce6577b3234295 to your computer and use it in GitHub Desktop.
flycheck-rust-xargo
;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with C-x C-f and enter text in its buffer.
(require 'flycheck)
(flycheck-define-checker rust-xargo
"A Rust syntax checker using Cargo.
This syntax checker requires Rust 1.17 or newer. See URL
`https://www.rust-lang.org'."
:command ("xargo"
(eval (if flycheck-rust-check-tests
"test"
"check"))
(eval (when flycheck-rust-check-tests
"--no-run"))
(eval (when flycheck-rust-crate-type
(concat "--" flycheck-rust-crate-type)))
;; All crate targets except "lib" need a binary name
(eval (when (and flycheck-rust-crate-type
(not (string= flycheck-rust-crate-type "lib")))
flycheck-rust-binary-name))
(eval flycheck-cargo-check-args)
"--message-format=json")
:error-parser flycheck-parse-cargo-rustc
:error-filter (lambda (errors)
;; In Rust 1.25+, filenames are relative to the workspace
;; root.
(let ((root (flycheck-rust-cargo-workspace-root)))
(seq-do (lambda (err)
(setf (flycheck-error-filename err)
(expand-file-name
(flycheck-error-filename err) root)))
(flycheck-rust-error-filter errors))))
:error-explainer flycheck-rust-error-explainer
:modes rust-mode
:predicate flycheck-buffer-saved-p
:enabled flycheck-rust-manifest-directory
:working-directory (lambda (_) (flycheck-rust-manifest-directory))
:verify
(lambda (_)
(and buffer-file-name
(let* ((has-toml (flycheck-rust-manifest-directory))
(valid-crate-type (flycheck-rust-valid-crate-type-p
flycheck-rust-crate-type))
(need-binary-name
(and flycheck-rust-crate-type
(not (string= flycheck-rust-crate-type "lib")))))
(list
(flycheck-verification-result-new
:label "Cargo.toml"
:message (if has-toml "Found" "Missing")
:face (if has-toml 'success '(bold warning)))
(flycheck-verification-result-new
:label "Crate type"
:message (if valid-crate-type
(format "%s" flycheck-rust-crate-type)
(format "%s (invalid, should be one of 'lib', 'bin', 'test', 'example' or 'bench')"
flycheck-rust-crate-type))
:face (if valid-crate-type 'success '(bold error)))
(flycheck-verification-result-new
:label "Binary name"
:message (cond
((not need-binary-name) "Not required")
((not flycheck-rust-binary-name) "Required")
(t (format "%s" flycheck-rust-binary-name)))
:face (cond
((not need-binary-name) 'success)
((not flycheck-rust-binary-name) '(bold error))
(t 'success))))))))
(provides 'flycheck-rust-xargo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment