trailofbits / dylint

Run Rust lints from dynamic libraries

Home Page:https://blog.trailofbits.com/2021/11/09/write-rust-lints-without-forking-clippy/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unresolved import when using external crate in tests

R9295 opened this issue · comments

commented

Hello,

I'm writing a dylint rule which checks for a specific function call derived from an external trait. I'm getting an unresolved import error when using an external crate in my tests.

Here's the test I'm writing (paraphrased)

# ui/main.rs
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
struct Test {
    a: u32,
    b: u64,
}

fn main() {}

When I run cargo test I get:

error[E0432]: unresolved import `serde`
  --> $DIR/main.rs:1:5
   |
LL | use serde::{Deserialize, Serialize};
   |     ^^^^^ maybe a missing crate `serde`?
   |
   = help: consider adding `extern crate serde` to use the `serde` crate

I've looked at your examples, and used extern crate but was unable to get it to work.

Is there a way to use external crates in tests?

Hi, @R9295. Does your test use dylint_testing::ui_test_example? If so, then it should be possible to declare external crates as dev-dependencies. For comparison, here is a lint that uses anyhow in this way:

[[example]]
name = "ui"
path = "ui/main.rs"

[dev-dependencies]
anyhow = "1.0"

#[test]
fn ui() {
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "ui");
}

commented

Ahhh, I was using the default dylint_testing::ui_test instead of ui_test_example.

Thanks for the quick response. Feel free to close the issue

This is good feedback. Thank you.

I will try to make mention of ui_test_example more prominent in the documentation.

Cheers.