2018-10-22 13:39:51 +00:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate ::rustc ::lint ::{ EarlyContext , EarlyLintPass , LintArray , LintPass } ;
use crate ::rustc ::{ declare_tool_lint , lint_array } ;
2018-10-24 11:22:38 +00:00
use crate ::syntax ::{ ast ::* , source_map ::DUMMY_SP } ;
2018-10-22 13:39:51 +00:00
use crate ::utils ::span_lint ;
use cargo_metadata ;
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 )
}
}
impl EarlyLintPass for Pass {
fn check_crate ( & mut self , cx : & EarlyContext < '_ > , krate : & Crate ) {
let metadata = if let Ok ( metadata ) = cargo_metadata ::metadata ( None ) {
metadata
} else {
span_lint ( cx , WILDCARD_DEPENDENCIES , krate . span , " 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 let Ok ( wildcard_ver ) = semver ::VersionReq ::parse ( " * " ) {
if dep . req = = wildcard_ver {
span_lint (
cx ,
WILDCARD_DEPENDENCIES ,
DUMMY_SP ,
& format! ( " wildcard dependency for ` {} ` " , dep . name ) ,
) ;
}
2018-10-22 13:39:51 +00:00
}
}
}
}