Skip to content

Commit 7e5fc78

Browse files
committed
Handle custom library crate-type
Closes GH-54.
1 parent a5491da commit 7e5fc78

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

flycheck-rust.el

+24-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@
3838

3939
;;; Code:
4040

41+
(eval-when-compile
42+
(require 'pcase)
43+
(require 'let-alist))
44+
4145
(require 'dash)
4246
(require 'flycheck)
4347
(require 'seq)
4448
(require 'json)
45-
(require 'let-alist)
4649

4750
(defun flycheck-rust-find-manifest (file-name)
4851
"Get the Cargo.toml manifest for FILE-NAME.
@@ -146,7 +149,26 @@ description of the conventional Cargo project layout."
146149
(file-name-directory manifest)))))
147150
;; If all else fails, just pick the first target
148151
(car targets))))
149-
(let-alist target (cons (car .kind) .name)))))
152+
(let-alist target (cons (flycheck-rust-normalize-target-kind .kind) .name)))))
153+
154+
(defun flycheck-rust-normalize-target-kind (kinds)
155+
"Return the normalized target name from KIND.
156+
157+
KIND is a list of target name as returned by `cargo metadata',
158+
which do not necessarily correspond to to target names that can
159+
be passed as argument to `cargo rustc'.
160+
161+
The normalization returns a valid cargo target based on KINDS."
162+
;; Assumption: we only care about the first kind name. Multiple kinds only
163+
;; seem to happen for library crate types, and those all maps to the same
164+
;; `lib' target.
165+
(pcase (car kinds)
166+
(`"dylib" "lib")
167+
(`"rlib" "lib")
168+
(`"staticlib" "lib")
169+
(`"cdylib" "lib")
170+
(`"proc-macro" "lib")
171+
(_ (car kinds))))
150172

151173
;;;###autoload
152174
(defun flycheck-rust-setup ()

tests/custom-lib-target/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "custom-lib-target"
3+
version = "0.1.0"
4+
5+
[lib]
6+
name = "foo"
7+
crate-type = ["dylib", "cdylib"]

tests/custom-lib-target/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn it_works() {
5+
}
6+
}

tests/test-rust-setup.el

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
(defun crate-file (file-name)
3636
(expand-file-name file-name "tests/test-crate"))
3737

38+
(defun lib-crate-file (file-name)
39+
(expand-file-name file-name "tests/custom-lib-target"))
40+
3841
(describe
3942
"`flycheck-rust-find-cargo-target' associates"
4043

@@ -107,4 +110,9 @@
107110
(expect
108111
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
109112
:to-equal-one-of '("bench" . "a") '("bench" . "b")))
113+
114+
(it "'src/lib.rs' to the library target"
115+
(expect
116+
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
117+
:to-equal "lib"))
110118
)

0 commit comments

Comments
 (0)