Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0263ddd

Browse files
committed
Lint for wildcard dependencies in Cargo.toml
1 parent 3ff2c1f commit 0263ddd

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub mod unused_label;
200200
pub mod unwrap;
201201
pub mod use_self;
202202
pub mod vec;
203+
pub mod wildcard_dependencies;
203204
pub mod write;
204205
pub mod zero_div_zero;
205206
// end lints modules, do not remove this comment, it’s used in `update_lints`
@@ -438,6 +439,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
438439
reg.register_late_lint_pass(box question_mark::Pass);
439440
reg.register_late_lint_pass(box suspicious_trait_impl::SuspiciousImpl);
440441
reg.register_early_lint_pass(box multiple_crate_versions::Pass);
442+
reg.register_early_lint_pass(box wildcard_dependencies::Pass);
441443
reg.register_late_lint_pass(box map_unit_fn::Pass);
442444
reg.register_late_lint_pass(box infallible_destructuring_match::Pass);
443445
reg.register_late_lint_pass(box inherent_impl::Pass::default());
@@ -967,6 +969,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
967969

968970
reg.register_lint_group("clippy::cargo", Some("clippy_cargo"), vec![
969971
multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
972+
wildcard_dependencies::WILDCARD_DEPENDENCIES,
970973
]);
971974

972975
reg.register_lint_group("clippy::nursery", Some("clippy_nursery"), vec![
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
11+
use crate::rustc::{declare_tool_lint, lint_array};
12+
use crate::syntax::ast::*;
13+
use crate::utils::span_lint;
14+
15+
use cargo_metadata;
16+
use lazy_static::lazy_static;
17+
use semver;
18+
19+
/// **What it does:** Checks to see if wildcard dependencies are being used.
20+
///
21+
/// **Why is this bad?** [As the edition guide sais](https://rust-lang-nursery.github.io/edition-guide/rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.html),
22+
/// it is highly unlikely that you work with any possible version of your dependency,
23+
/// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
24+
///
25+
/// **Known problems:** None.
26+
///
27+
/// **Example:**
28+
/// ```toml
29+
/// [dependencies]
30+
/// regex = "*"
31+
/// ```
32+
declare_clippy_lint! {
33+
pub WILDCARD_DEPENDENCIES,
34+
cargo,
35+
"wildcard dependencies being used"
36+
}
37+
38+
pub struct Pass;
39+
40+
impl LintPass for Pass {
41+
fn get_lints(&self) -> LintArray {
42+
lint_array!(WILDCARD_DEPENDENCIES)
43+
}
44+
}
45+
46+
impl EarlyLintPass for Pass {
47+
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
48+
let metadata = if let Ok(metadata) = cargo_metadata::metadata(None) {
49+
metadata
50+
} else {
51+
span_lint(cx, WILDCARD_DEPENDENCIES, krate.span, "could not read cargo metadata");
52+
return;
53+
};
54+
55+
lazy_static! {
56+
static ref WILDCARD_VERSION_REQ: semver::VersionReq = semver::VersionReq::parse("*").unwrap();
57+
}
58+
59+
for dep in &metadata.packages[0].dependencies {
60+
if dep.req == *WILDCARD_VERSION_REQ {
61+
span_lint(
62+
cx,
63+
WILDCARD_DEPENDENCIES,
64+
krate.span,
65+
&format!("wildcard dependency for `{}`", dep.name),
66+
);
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)