2021-07-09 13:06:12 +00:00
|
|
|
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
|
2020-04-21 20:53:18 +00:00
|
|
|
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::DUMMY_SP;
|
2018-10-22 13:39:51 +00:00
|
|
|
|
2018-12-04 05:47:41 +00:00
|
|
|
use if_chain::if_chain;
|
2018-10-22 13:39:51 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for wildcard dependencies in the `Cargo.toml`.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### 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),
|
2019-03-05 16:50:33 +00:00
|
|
|
/// it is highly unlikely that you work with any possible version of your dependency,
|
|
|
|
/// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```toml
|
|
|
|
/// [dependencies]
|
|
|
|
/// regex = "*"
|
|
|
|
/// ```
|
2018-10-22 13:39:51 +00:00
|
|
|
pub WILDCARD_DEPENDENCIES,
|
|
|
|
cargo,
|
|
|
|
"wildcard dependencies being used"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
|
2018-10-22 13:39:51 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl LateLintPass<'_> for WildcardDependencies {
|
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
2021-07-09 13:06:12 +00:00
|
|
|
if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) {
|
2020-04-21 20:53:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:36:01 +00:00
|
|
|
let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
|
2018-10-22 13:39:51 +00:00
|
|
|
|
|
|
|
for dep in &metadata.packages[0].dependencies {
|
2018-10-24 11:18:19 +00:00
|
|
|
// VersionReq::any() does not work
|
2018-12-03 07:12:35 +00:00
|
|
|
if_chain! {
|
|
|
|
if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
|
|
|
|
if let Some(ref source) = dep.source;
|
|
|
|
if !source.starts_with("git");
|
|
|
|
if dep.req == wildcard_ver;
|
|
|
|
then {
|
2018-10-24 11:18:19 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
WILDCARD_DEPENDENCIES,
|
|
|
|
DUMMY_SP,
|
|
|
|
&format!("wildcard dependency for `{}`", dep.name),
|
|
|
|
);
|
|
|
|
}
|
2018-10-22 13:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|