1 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
2 use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
5 use rustc_span::source_map::DUMMY_SP;
7 use if_chain::if_chain;
11 /// Checks for wildcard dependencies in the `Cargo.toml`.
13 /// ### Why is this bad?
14 /// [As the edition guide says](https://rust-lang-nursery.github.io/edition-guide/rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.html),
15 /// it is highly unlikely that you work with any possible version of your dependency,
16 /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
23 pub WILDCARD_DEPENDENCIES,
25 "wildcard dependencies being used"
28 declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
30 impl LateLintPass<'_> for WildcardDependencies {
31 fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
32 if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) {
36 let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
38 for dep in &metadata.packages[0].dependencies {
39 // VersionReq::any() does not work
41 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
42 if let Some(ref source) = dep.source;
43 if !source.starts_with("git");
44 if dep.req == wildcard_ver;
48 WILDCARD_DEPENDENCIES,
50 &format!("wildcard dependency for `{}`", dep.name),