Skip to content

Commit 319b75c

Browse files
bors[bot]ordovicia
andcommitted
Merge #3349
3349: Fixes #3347: Lint for wildcard dependencies in Cargo.toml r=ordovicia a=ordovicia Add a lint for wildcard dependencies in Cargo.toml. How should I write a test for this lint? Fixes #3347 Co-authored-by: Hidehito Yabuuchi <[email protected]>
2 parents 03f8899 + 99b78f0 commit 319b75c

File tree

5 files changed

+77
-10
lines changed

5 files changed

+77
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ All notable changes to this project will be documented in this file.
893893
[`while_immutable_condition`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_immutable_condition
894894
[`while_let_loop`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_let_loop
895895
[`while_let_on_iterator`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_let_on_iterator
896+
[`wildcard_dependencies`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#wildcard_dependencies
896897
[`write_literal`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#write_literal
897898
[`write_with_newline`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#write_with_newline
898899
[`writeln_empty_string`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#writeln_empty_string

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ We are currently in the process of discussing Clippy 1.0 via the RFC process in
99

1010
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
1111

12-
[There are 280 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
12+
[There are 281 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
1313

1414
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1515

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![

clippy_lints/src/multiple_crate_versions.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
1110
//! lint on multiple versions of a crate being used
1211
1312
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
1413
use crate::rustc::{declare_tool_lint, lint_array};
15-
use crate::syntax::ast::*;
14+
use crate::syntax::{ast::*, source_map::DUMMY_SP};
1615
use crate::utils::span_lint;
1716

1817
use cargo_metadata;
@@ -54,12 +53,7 @@ impl EarlyLintPass for Pass {
5453
let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
5554
metadata
5655
} else {
57-
span_lint(
58-
cx,
59-
MULTIPLE_CRATE_VERSIONS,
60-
krate.span,
61-
"could not read cargo metadata"
62-
);
56+
span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
6357

6458
return;
6559
};
@@ -76,7 +70,7 @@ impl EarlyLintPass for Pass {
7670
span_lint(
7771
cx,
7872
MULTIPLE_CRATE_VERSIONS,
79-
krate.span,
73+
DUMMY_SP,
8074
&format!("multiple versions for dependency `{}`: {}", name, versions),
8175
);
8276
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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::*, source_map::DUMMY_SP};
13+
use crate::utils::span_lint;
14+
15+
use cargo_metadata;
16+
use semver;
17+
18+
/// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
19+
///
20+
/// **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),
21+
/// it is highly unlikely that you work with any possible version of your dependency,
22+
/// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
23+
///
24+
/// **Known problems:** None.
25+
///
26+
/// **Example:**
27+
///
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, DUMMY_SP, "could not read cargo metadata");
52+
return;
53+
};
54+
55+
for dep in &metadata.packages[0].dependencies {
56+
// VersionReq::any() does not work
57+
if let Ok(wildcard_ver) = semver::VersionReq::parse("*") {
58+
if dep.req == wildcard_ver {
59+
span_lint(
60+
cx,
61+
WILDCARD_DEPENDENCIES,
62+
DUMMY_SP,
63+
&format!("wildcard dependency for `{}`", dep.name),
64+
);
65+
}
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)