Skip to content

Commit 72c479b

Browse files
committed
Add rust-run-clippy' and rust-buffer-project' with testing paraphernalia.
1 parent 60a1f36 commit 72c479b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

rust-mode-tests.el

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,15 @@ extern \"rust-intrinsic\" fn five() {
26502650
"four"
26512651
"five"))))
26522652

2653+
(ert-deftest rust-test-project-located ()
2654+
(lexical-let* ((this-dir default-directory)
2655+
(test-dir (expand-file-name "test-project" this-dir))
2656+
(manifest-file (expand-file-name "Cargo.toml" test-dir)))
2657+
(find-file (expand-file-name "test-project/foo.rs"))
2658+
(unwind-protect
2659+
(should (equal (expand-file-name (rust-buffer-project)) manifest-file))
2660+
(kill-buffer))))
2661+
26532662
;; If electric-pair-mode is available, load it and run the tests that use it. If not,
26542663
;; no error--the tests will be skipped.
26552664
(require 'elec-pair nil t)

rust-mode.el

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ function or trait. When nil, where will be aligned with fn or trait."
144144
:type 'string
145145
:group 'rust-mode)
146146

147+
(defcustom rust-always-locate-project-on-open nil
148+
"Whether to run `cargo locate-project' every time `rust-mode'
149+
is activated."
150+
:type 'boolean
151+
:group 'rust-mode)
152+
147153
(defface rust-unsafe-face
148154
'((t :inherit font-lock-warning-face))
149155
"Face for the `unsafe' keyword."
@@ -1411,7 +1417,12 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
14111417

14121418
(setq-local compile-command "cargo build")
14131419

1414-
(add-hook 'before-save-hook 'rust--before-save-hook nil t))
1420+
(add-hook 'before-save-hook 'rust--before-save-hook nil t)
1421+
1422+
(setq-local rust-buffer-project nil)
1423+
1424+
(when rust-always-locate-project-on-open
1425+
(rust-update-buffer-project)))
14151426

14161427
;;;###autoload
14171428
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
@@ -1548,6 +1559,30 @@ visit the new file."
15481559
(rename-file filename new-name 1)
15491560
(set-visited-file-name new-name))))))
15501561

1562+
(defun rust-run-clippy ()
1563+
"Run `cargo clippy'."
1564+
(interactive)
1565+
(when (null rust-buffer-project)
1566+
(rust-update-buffer-project))
1567+
(let* ((args (list "cargo" "clippy" (concat "--manifest-path=" rust-buffer-project)))
1568+
;; set `compile-command' temporarily so `compile' doesn't
1569+
;; clobber the existing value
1570+
(compile-command (mapconcat #'shell-quote-argument args " ")))
1571+
(compile compile-command)))
1572+
1573+
(defun rust-update-buffer-project ()
1574+
(setq-local rust-buffer-project (rust-buffer-project)))
1575+
1576+
(defun rust-buffer-project ()
1577+
"Get project root if possible."
1578+
(with-temp-buffer
1579+
(let ((ret (call-process "cargo" nil t nil "locate-project")))
1580+
(when (/= ret 0)
1581+
(error "`cargo locate-project' returned %s status: %s" ret (buffer-string)))
1582+
(goto-char 0)
1583+
(let ((output (json-read)))
1584+
(cdr (assoc-string "root" output))))))
1585+
15511586
(provide 'rust-mode)
15521587

15531588
;;; rust-mode.el ends here

test-project/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

0 commit comments

Comments
 (0)