1 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
2 use rustc_hir::hir_id::CRATE_HIR_ID;
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 #[clippy::version = "1.32.0"]
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<'_>) {
33 if is_lint_allowed(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),