Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused_crate_dependencies false positives when dependency used only in doctests #78346

Closed
Hawk777 opened this issue Oct 25, 2020 · 4 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@Hawk777
Copy link

Hawk777 commented Oct 25, 2020

If a dependency is only used in a doctest, it triggers an unused_crate_dependencies false positive.

I tried this code:

main.rs:

#![warn(unused_crate_dependencies)]

///! # Example
///! ```
///! use gcd::Gcd;
///! assert_eq!(6u32.gcd(8u32), 2u32);
///! ```

fn main() {
}

cargo.Toml:

[package]
name = "foo"
version = "0.1.0"
edition = "2018"

[dev-dependencies.gcd]
version = "^2.0"

I expected to see this happen: No warnings.

Instead, this happened: cargo test generates an unused_crate_dependencies warning for the gcd crate.

Meta

rustc --version --verbose:

rustc 1.46.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.46.0
LLVM version: 10.0
@Hawk777 Hawk777 added the C-bug Category: This is a bug. label Oct 25, 2020
@est31
Copy link
Member

est31 commented Oct 25, 2020

The unused_crate_dependencies lint for now isn't meant for cargo users for now, but for users of build tools like buck.

For a fix, cargo needs to be the party that emits the lint, not rustc. See my PR rust-lang/cargo#8437

@Hawk777
Copy link
Author

Hawk777 commented Oct 25, 2020

Oh, I had no idea. I saw it in the list and thought it was a reasonable way for an ordinary person writing a Rust library or program to be made aware of crates they accidentally depended on but then later stopped using. From your description in that PR, it sounds like once the warning is ready it will be enabled by default, so I should remove the #![warn(unused_crate_dependencies)] from my source; is that right?

@est31
Copy link
Member

est31 commented Oct 25, 2020

Yes, once the PR is merged, the lint itself is suppressed on the rustc side and instead rustc sends info that cargo can digest, and then cargo emits the lint instead. The level is still sent by rustc so you can still control it with adding a #![warn(...)] thing. Also yes, the goal is to eventually enable it by default, but I want to be a bit more careful with it and first let users opt into it. Then later once it's known that there are no bugs, it can be enabled.

You can remove the warn, or keep it in, it's your choice.

@Hawk777
Copy link
Author

Hawk777 commented Oct 25, 2020

Thanks for clarifying. I think it makes sense to close this issue now.

@Hawk777 Hawk777 closed this as completed Oct 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants