“cargo check”对仅由测试使用的函数给出死代码警告

HC4*_*ica 5 compiler-warnings rust rust-cargo

在下面的代码中:

mod foo {
    pub fn bar() -> i32 { 42 }
}

#[cfg(test)]
mod tests {
    use foo;

    #[test]
    fn test_bar() {
        assert_eq!(42, foo::bar());
    }
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

该函数bar()仅由测试代码使用。

cargo check给出此代码的死代码警告:

mod foo {
    pub fn bar() -> i32 { 42 }
}

#[cfg(test)]
mod tests {
    use foo;

    #[test]
    fn test_bar() {
        assert_eq!(42, foo::bar());
    }
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

我发现“死代码”警告通常很有用,但我只希望为真正未使用的函数(包括测试代码)生成它们。有没有办法让自己cargo check有这样的行为?