diff options
| author | Graham Marlow <graham@onesignal.com> | 2023-06-13 16:29:04 -0700 |
|---|---|---|
| committer | Graham Marlow <graham@onesignal.com> | 2023-06-13 16:29:04 -0700 |
| commit | ec28fa8b7673db15c346cc0db45d159c15fe8ab4 (patch) | |
| tree | ecf308271d4bffc30d37d4f543398e4fb649c055 | |
| parent | 708cf774693243dbc5945fa8e764f1da65ae4ef9 (diff) | |
| download | flymake-clippy-ec28fa8b7673db15c346cc0db45d159c15fe8ab4.tar.gz | |
Rename variable prefix and add tests for regexp
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | clippy-flymake-test.el | 31 | ||||
| -rw-r--r-- | clippy-flymake.el | 22 | ||||
| -rw-r--r-- | test/fixture.txt | 21 |
4 files changed, 67 insertions, 12 deletions
@@ -1,4 +1,7 @@ -.PHONY: build clean +.PHONY: build test clean + +test: build + emacs -batch -l ert -L . -l clippy-flymake-test.el -f ert-run-tests-batch-and-exit build: clean emacs -batch -L . -f batch-byte-compile clippy-flymake.el diff --git a/clippy-flymake-test.el b/clippy-flymake-test.el new file mode 100644 index 0000000..f33db6c --- /dev/null +++ b/clippy-flymake-test.el @@ -0,0 +1,31 @@ +;;; clippy-flymake-test.el -*- lexical-binding: t; -*- + +(require 'clippy-flymake) +(require 'ert) + +(defun run-regexp (filename) + (set-match-data nil) + (search-forward-regexp (clippy-flymake--build-regexp filename) nil t) + (list (match-string 1) + (match-string 2) + (match-string 3))) + +(ert-deftest clippy-test-regexp () + "Tests regexp matches diagnostic information." + (should (equal (with-temp-buffer + (insert-file-contents "./test/fixture.txt") + (run-regexp "foo.rs")) + '("warning: unused variable: `user`" " --> src/database/foo.rs" "42"))) + (should (equal (with-temp-buffer + (insert-file-contents "./test/fixture.txt") + (run-regexp "foo.rs") + (run-regexp "foo.rs")) + '("warning: using `clone` on type `Status` which implements the `Copy` trait" " --> src/foo.rs" "31"))) + (should (equal (with-temp-buffer + (insert-file-contents "./test/fixture.txt") + (run-regexp "user.rs")) + '("warning: unused variable: `user`" " --> src/foobar/user.rs" "42"))) + (should (equal (with-temp-buffer + (insert-file-contents "./test/fixture.txt") + (run-regexp "notfound.rs")) + '(nil nil nil)))) diff --git a/clippy-flymake.el b/clippy-flymake.el index 42bc56a..b61abd4 100644 --- a/clippy-flymake.el +++ b/clippy-flymake.el @@ -31,7 +31,7 @@ ;; "warning: ..." ;; --> src/filename.rs ;; 98 | ... -(defun clippy--build-regexp (filename) +(defun clippy-flymake--build-regexp (filename) "Create a regular expression to search Clippy warnings for FILENAME." (rx-to-string `(seq line-start @@ -55,10 +55,10 @@ (any "0-9"))) line-end))) -(defvar clippy--flymake-proc nil +(defvar clippy-flymake--proc nil "Clippy subprocess object, used to ensure obsolete processes aren't reused.") -(defun clippy-flymake (report-fn &rest _args) +(defun clippy-flymake--check-buffer (report-fn &rest _args) "Flymake backend for cargo clippy. REPORT-FN is passed in via `flymake-diagnostic-functions' hook. @@ -71,7 +71,7 @@ with the appropriate Flymake hook." (filename (file-name-nondirectory (buffer-file-name source)))) (save-restriction (widen) - (setq clippy--flymake-proc + (setq clippy-flymake--proc (make-process :name "clippy-flymake" :noquery t :connection-type 'pipe :buffer (generate-new-buffer "*clippy-flymake*") @@ -80,14 +80,14 @@ with the appropriate Flymake hook." (lambda (proc _event) (when (memq (process-status proc) '(exit signal)) (unwind-protect - (if (with-current-buffer source (eq proc clippy--flymake-proc)) + (if (with-current-buffer source (eq proc clippy-flymake--proc)) (with-current-buffer (process-buffer proc) (goto-char (point-min)) ;; Collect output buffer into diagnostic messages/locations, ;; exposing them via `report-fn'. (cl-loop while (search-forward-regexp - (clippy--build-regexp filename) + (clippy-flymake--build-regexp filename) nil t) for msg = (match-string 1) for (beg . end) = (flymake-diag-region @@ -102,12 +102,12 @@ with the appropriate Flymake hook." (flymake-log :warning "Canceling obsolete check %s" proc)) ;; Cleanup temp buffer. (kill-buffer (process-buffer proc))))))) - (process-send-region clippy--flymake-proc (point-min) (point-max)) - (process-send-eof clippy--flymake-proc)))) + (process-send-region clippy-flymake--proc (point-min) (point-max)) + (process-send-eof clippy-flymake--proc)))) (defun clippy-flymake-setup-backend () "Add `clippy-flymake' to `flymake-diagnostic-functions' hook." - (add-hook 'flymake-diagnostic-functions #'clippy-flymake nil t)) + (add-hook 'flymake-diagnostic-functions #'clippy-flymake--check-buffer nil t)) -(provide 'flymake-clippy) -;;; flymake-clippy.el ends here +(provide 'clippy-flymake) +;;; clippy-flymake.el ends here diff --git a/test/fixture.txt b/test/fixture.txt new file mode 100644 index 0000000..9bbcbea --- /dev/null +++ b/test/fixture.txt @@ -0,0 +1,21 @@ +warning: unused variable: `user` + --> src/database/foo.rs:42:9 + | +42 | user: &User, + | ^^^^ help: if this is intentional, prefix it with an underscore: `_user` + | + = note: `#[warn(unused_variables)]` on by default + +warning: using `clone` on type `Status` which implements the `Copy` trait + --> src/foo.rs:31:29 + | +31 | Some(status) => status.clone(), + | ^^^^^^^^^^^^^^ help: try dereferencing it: `*status` + +warning: unused variable: `user` + --> src/foobar/user.rs:42:9 + | +42 | user: &User, + | ^^^^ help: if this is intentional, prefix it with an underscore: `_user` + | + = note: `#[warn(unused_variables)]` on by default |
