2018-10-22 13:39:51 +00:00
use crate ::utils ::span_lint ;
2018-12-29 15:04:45 +00:00
use rustc ::lint ::{ EarlyContext , EarlyLintPass , LintArray , LintPass } ;
use rustc ::{ declare_tool_lint , lint_array } ;
use syntax ::{ ast ::* , source_map ::DUMMY_SP } ;
2018-10-22 13:39:51 +00:00
use cargo_metadata ;
2018-12-04 05:47:41 +00:00
use if_chain ::if_chain ;
2018-10-22 13:39:51 +00:00
use semver ;
2018-10-24 11:18:19 +00:00
/// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
2018-10-22 13:39:51 +00:00
///
2018-10-24 11:18:19 +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),
2018-10-22 13:39:51 +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.
///
/// **Known problems:** None.
///
/// **Example:**
2018-10-24 02:34:36 +00:00
///
2018-10-22 13:39:51 +00:00
/// ```toml
/// [dependencies]
/// regex = "*"
/// ```
declare_clippy_lint! {
pub WILDCARD_DEPENDENCIES ,
cargo ,
" wildcard dependencies being used "
}
pub struct Pass ;
impl LintPass for Pass {
fn get_lints ( & self ) -> LintArray {
lint_array! ( WILDCARD_DEPENDENCIES )
}
2019-01-26 19:40:55 +00:00
fn name ( & self ) -> & 'static str {
" WildcardDependencies "
}
2018-10-22 13:39:51 +00:00
}
impl EarlyLintPass for Pass {
2018-10-24 14:18:01 +00:00
fn check_crate ( & mut self , cx : & EarlyContext < '_ > , _ : & Crate ) {
2019-01-25 19:27:07 +00:00
let metadata = if let Ok ( metadata ) = cargo_metadata ::MetadataCommand ::new ( ) . no_deps ( ) . exec ( ) {
2018-10-22 13:39:51 +00:00
metadata
} else {
2018-10-24 12:15:27 +00:00
span_lint ( cx , WILDCARD_DEPENDENCIES , DUMMY_SP , " could not read cargo metadata " ) ;
2018-10-22 13:39:51 +00:00
return ;
} ;
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
}
}
}
}