rust-clippy/clippy_lints/src/wildcard_dependencies.rs

60 lines
2 KiB
Rust
Raw Normal View History

use crate::utils::span_lint;
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
2019-04-08 20:43:55 +00:00
use rustc::{declare_lint_pass, declare_tool_lint};
use syntax::{ast::*, source_map::DUMMY_SP};
use cargo_metadata;
2018-12-04 05:47:41 +00:00
use if_chain::if_chain;
use semver;
declare_clippy_lint! {
/// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
///
/// **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),
/// it is highly unlikely that you work with any possible version of your dependency,
/// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```toml
/// [dependencies]
/// regex = "*"
/// ```
pub WILDCARD_DEPENDENCIES,
cargo,
"wildcard dependencies being used"
}
2019-04-08 20:43:55 +00:00
declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
2019-04-08 20:43:55 +00:00
impl EarlyLintPass for WildcardDependencies {
2018-10-24 14:18:01 +00:00
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
metadata
} else {
span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
return;
};
for dep in &metadata.packages[0].dependencies {
2018-10-24 11:18:19 +00:00
// VersionReq::any() does not work
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),
);
}
}
}
}
}