1 use crate::utils::{run_lints, span_lint};
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;
10 /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
12 /// **Why is this bad?** [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),
13 /// it is highly unlikely that you work with any possible version of your dependency,
14 /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
16 /// **Known problems:** None.
24 pub WILDCARD_DEPENDENCIES,
26 "wildcard dependencies being used"
29 declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
31 impl LateLintPass<'_, '_> for WildcardDependencies {
32 fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
33 if !run_lints(cx, &[WILDCARD_DEPENDENCIES], CRATE_HIR_ID) {
37 let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
39 for dep in &metadata.packages[0].dependencies {
40 // VersionReq::any() does not work
42 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
43 if let Some(ref source) = dep.source;
44 if !source.starts_with("git");
45 if dep.req == wildcard_ver;
49 WILDCARD_DEPENDENCIES,
51 &format!("wildcard dependency for `{}`", dep.name),