summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormgmarlow <graham@mgmarlow.com>2023-06-13 19:09:04 -0700
committermgmarlow <graham@mgmarlow.com>2023-06-13 19:09:04 -0700
commit30caf05855122ab117635afdf920d64659c927aa (patch)
tree2833e5b60770f7ea2b7ef47d016461da077dcc22
parenta93359027caeb163aaa09df674e4e039313c9164 (diff)
downloadflymake-clippy-30caf05855122ab117635afdf920d64659c927aa.tar.gz
Improve filename matching
Rather than matching filenames within the regular expression (which was using file-name-nondirectory), capture the full file name only in the regexp so it can be compared against the entire buffer-file-name. This fixes issues where Clippy suggestions with the same file name (e.g. mod.rs) appear in every buffer matching that name.
-rw-r--r--.gitignore1
-rw-r--r--clippy-flymake-test.el24
-rw-r--r--clippy-flymake.el13
3 files changed, 20 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c531d98
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.elc
diff --git a/clippy-flymake-test.el b/clippy-flymake-test.el
index f33db6c..6039824 100644
--- a/clippy-flymake-test.el
+++ b/clippy-flymake-test.el
@@ -3,9 +3,9 @@
(require 'clippy-flymake)
(require 'ert)
-(defun run-regexp (filename)
+(defun run-regexp ()
(set-match-data nil)
- (search-forward-regexp (clippy-flymake--build-regexp filename) nil t)
+ (search-forward-regexp (clippy-flymake--build-regexp) nil t)
(list (match-string 1)
(match-string 2)
(match-string 3)))
@@ -14,18 +14,16 @@
"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")))
+ (run-regexp))
+ '("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")))
+ (run-regexp)
+ (run-regexp))
+ '("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))))
+ (run-regexp)
+ (run-regexp)
+ (run-regexp))
+ '("warning: unused variable: `user`" "src/foobar/user.rs" "42"))))
diff --git a/clippy-flymake.el b/clippy-flymake.el
index b61abd4..dc73fac 100644
--- a/clippy-flymake.el
+++ b/clippy-flymake.el
@@ -31,7 +31,7 @@
;; "warning: ..."
;; --> src/filename.rs
;; 98 | ...
-(defun clippy-flymake--build-regexp (filename)
+(defun clippy-flymake--build-regexp ()
"Create a regular expression to search Clippy warnings for FILENAME."
(rx-to-string
`(seq line-start
@@ -39,10 +39,11 @@
(group "warning:"
(zero-or-more nonl))
"\n"
+ (zero-or-more nonl)
+ "--> "
;; File
(group
- (zero-or-more nonl)
- nonl ,filename)
+ (zero-or-more nonl))
":"
;; Line
(group
@@ -68,7 +69,7 @@ with the appropriate Flymake hook."
(error "Cannot find cargo"))
(let* ((source (current-buffer))
- (filename (file-name-nondirectory (buffer-file-name source))))
+ (filename (buffer-file-name source)))
(save-restriction
(widen)
(setq clippy-flymake--proc
@@ -87,15 +88,17 @@ with the appropriate Flymake hook."
;; exposing them via `report-fn'.
(cl-loop
while (search-forward-regexp
- (clippy-flymake--build-regexp filename)
+ (clippy-flymake--build-regexp)
nil t)
for msg = (match-string 1)
+ for sourcefile = (match-string 2)
for (beg . end) = (flymake-diag-region
source
(string-to-number (match-string 3)))
for type = (if (string-match "^warning" msg)
:warning
:error)
+ when (and sourcefile (string-match-p sourcefile filename))
collect (flymake-make-diagnostic source beg end type msg)
into diags
finally (funcall report-fn diags)))