From 0263ddde92d865a7e42dfe0d567286389d032faf Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Mon, 22 Oct 2018 22:39:51 +0900 Subject: [PATCH 1/6] Lint for wildcard dependencies in Cargo.toml --- clippy_lints/src/lib.rs | 3 + clippy_lints/src/wildcard_dependencies.rs | 70 +++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 clippy_lints/src/wildcard_dependencies.rs diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 23bd71a08ab6..b9f437a953f5 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -200,6 +200,7 @@ pub mod unused_label; pub mod unwrap; pub mod use_self; pub mod vec; +pub mod wildcard_dependencies; pub mod write; pub mod zero_div_zero; // 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) { reg.register_late_lint_pass(box question_mark::Pass); reg.register_late_lint_pass(box suspicious_trait_impl::SuspiciousImpl); reg.register_early_lint_pass(box multiple_crate_versions::Pass); + reg.register_early_lint_pass(box wildcard_dependencies::Pass); reg.register_late_lint_pass(box map_unit_fn::Pass); reg.register_late_lint_pass(box infallible_destructuring_match::Pass); 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) { reg.register_lint_group("clippy::cargo", Some("clippy_cargo"), vec![ multiple_crate_versions::MULTIPLE_CRATE_VERSIONS, + wildcard_dependencies::WILDCARD_DEPENDENCIES, ]); reg.register_lint_group("clippy::nursery", Some("clippy_nursery"), vec![ diff --git a/clippy_lints/src/wildcard_dependencies.rs b/clippy_lints/src/wildcard_dependencies.rs new file mode 100644 index 000000000000..3045130231c9 --- /dev/null +++ b/clippy_lints/src/wildcard_dependencies.rs @@ -0,0 +1,70 @@ +// 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 or the MIT license +// , 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}; +use crate::syntax::ast::*; +use crate::utils::span_lint; + +use cargo_metadata; +use lazy_static::lazy_static; +use semver; + +/// **What it does:** Checks to see if wildcard dependencies are being used. +/// +/// **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), +/// 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 = "*" +/// ``` +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; + }; + + lazy_static! { + static ref WILDCARD_VERSION_REQ: semver::VersionReq = semver::VersionReq::parse("*").unwrap(); + } + + for dep in &metadata.packages[0].dependencies { + if dep.req == *WILDCARD_VERSION_REQ { + span_lint( + cx, + WILDCARD_DEPENDENCIES, + krate.span, + &format!("wildcard dependency for `{}`", dep.name), + ); + } + } + } +} From fa6c9f838cbe5a50b8e7871c146cd1d8fe8e4e00 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Wed, 24 Oct 2018 11:34:36 +0900 Subject: [PATCH 2/6] Minor changes on clippy_lints/src/wildcard_dependencies.rs --- clippy_lints/src/wildcard_dependencies.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clippy_lints/src/wildcard_dependencies.rs b/clippy_lints/src/wildcard_dependencies.rs index 3045130231c9..e02501005e2b 100644 --- a/clippy_lints/src/wildcard_dependencies.rs +++ b/clippy_lints/src/wildcard_dependencies.rs @@ -25,6 +25,7 @@ use semver; /// **Known problems:** None. /// /// **Example:** +/// /// ```toml /// [dependencies] /// regex = "*" @@ -53,6 +54,7 @@ impl EarlyLintPass for Pass { }; lazy_static! { + // VersionReq::any() does not work static ref WILDCARD_VERSION_REQ: semver::VersionReq = semver::VersionReq::parse("*").unwrap(); } From d334fab4d0c28bd59538864e02847876a032439c Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Wed, 24 Oct 2018 14:59:19 +0900 Subject: [PATCH 3/6] Run util/update_lints.py --- CHANGELOG.md | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 626c39457e20..768751b2f080 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -893,6 +893,7 @@ All notable changes to this project will be documented in this file. [`while_immutable_condition`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_immutable_condition [`while_let_loop`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_let_loop [`while_let_on_iterator`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_let_on_iterator +[`wildcard_dependencies`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#wildcard_dependencies [`write_literal`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#write_literal [`write_with_newline`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#write_with_newline [`writeln_empty_string`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#writeln_empty_string diff --git a/README.md b/README.md index d32f66b59574..94fb8c6c02fd 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ We are currently in the process of discussing Clippy 1.0 via the RFC process in A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 280 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html) +[There are 281 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: From 663f2cff7e08301841dd212fcf945b706ca2f224 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Wed, 24 Oct 2018 20:18:19 +0900 Subject: [PATCH 4/6] Some fixes for wildcard_dependencies --- clippy_lints/src/wildcard_dependencies.rs | 28 +++++++++++------------ 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/wildcard_dependencies.rs b/clippy_lints/src/wildcard_dependencies.rs index e02501005e2b..00533efb8e5e 100644 --- a/clippy_lints/src/wildcard_dependencies.rs +++ b/clippy_lints/src/wildcard_dependencies.rs @@ -10,15 +10,15 @@ use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use crate::rustc::{declare_tool_lint, lint_array}; use crate::syntax::ast::*; +use crate::syntax::source_map::DUMMY_SP; use crate::utils::span_lint; use cargo_metadata; -use lazy_static::lazy_static; use semver; -/// **What it does:** Checks to see if wildcard dependencies are being used. +/// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`. /// -/// **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), +/// **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. /// @@ -53,19 +53,17 @@ impl EarlyLintPass for Pass { return; }; - lazy_static! { - // VersionReq::any() does not work - static ref WILDCARD_VERSION_REQ: semver::VersionReq = semver::VersionReq::parse("*").unwrap(); - } - for dep in &metadata.packages[0].dependencies { - if dep.req == *WILDCARD_VERSION_REQ { - span_lint( - cx, - WILDCARD_DEPENDENCIES, - krate.span, - &format!("wildcard dependency for `{}`", dep.name), - ); + // 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), + ); + } } } } From 0d577c36a9e48fce3b9a0a49b7d5d59f8710af35 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Wed, 24 Oct 2018 20:22:38 +0900 Subject: [PATCH 5/6] Use DUMMY_SP in multiple_crate_versions --- clippy_lints/src/multiple_crate_versions.rs | 12 +++--------- clippy_lints/src/wildcard_dependencies.rs | 3 +-- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/multiple_crate_versions.rs b/clippy_lints/src/multiple_crate_versions.rs index dbf8cbe16c23..9507522ed108 100644 --- a/clippy_lints/src/multiple_crate_versions.rs +++ b/clippy_lints/src/multiple_crate_versions.rs @@ -7,12 +7,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. - //! lint on multiple versions of a crate being used use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use crate::rustc::{declare_tool_lint, lint_array}; -use crate::syntax::ast::*; +use crate::syntax::{ast::*, source_map::DUMMY_SP}; use crate::utils::span_lint; use cargo_metadata; @@ -54,12 +53,7 @@ impl EarlyLintPass for Pass { let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) { metadata } else { - span_lint( - cx, - MULTIPLE_CRATE_VERSIONS, - krate.span, - "could not read cargo metadata" - ); + span_lint(cx, MULTIPLE_CRATE_VERSIONS, krate.span, "could not read cargo metadata"); return; }; @@ -76,7 +70,7 @@ impl EarlyLintPass for Pass { span_lint( cx, MULTIPLE_CRATE_VERSIONS, - krate.span, + DUMMY_SP, &format!("multiple versions for dependency `{}`: {}", name, versions), ); } diff --git a/clippy_lints/src/wildcard_dependencies.rs b/clippy_lints/src/wildcard_dependencies.rs index 00533efb8e5e..8f2f1aeb27a7 100644 --- a/clippy_lints/src/wildcard_dependencies.rs +++ b/clippy_lints/src/wildcard_dependencies.rs @@ -9,8 +9,7 @@ use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use crate::rustc::{declare_tool_lint, lint_array}; -use crate::syntax::ast::*; -use crate::syntax::source_map::DUMMY_SP; +use crate::syntax::{ast::*, source_map::DUMMY_SP}; use crate::utils::span_lint; use cargo_metadata; From 99b78f06506c4f04399d2186b434a5dd4e16e792 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Wed, 24 Oct 2018 21:15:27 +0900 Subject: [PATCH 6/6] Replace remaining `krate.span` with `DUMMY_SP` --- clippy_lints/src/multiple_crate_versions.rs | 2 +- clippy_lints/src/wildcard_dependencies.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/multiple_crate_versions.rs b/clippy_lints/src/multiple_crate_versions.rs index 9507522ed108..da1ccd8fdf07 100644 --- a/clippy_lints/src/multiple_crate_versions.rs +++ b/clippy_lints/src/multiple_crate_versions.rs @@ -53,7 +53,7 @@ impl EarlyLintPass for Pass { let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) { metadata } else { - span_lint(cx, MULTIPLE_CRATE_VERSIONS, krate.span, "could not read cargo metadata"); + span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata"); return; }; diff --git a/clippy_lints/src/wildcard_dependencies.rs b/clippy_lints/src/wildcard_dependencies.rs index 8f2f1aeb27a7..c172b38a6cbe 100644 --- a/clippy_lints/src/wildcard_dependencies.rs +++ b/clippy_lints/src/wildcard_dependencies.rs @@ -48,7 +48,7 @@ impl EarlyLintPass for Pass { let metadata = if let Ok(metadata) = cargo_metadata::metadata(None) { metadata } else { - span_lint(cx, WILDCARD_DEPENDENCIES, krate.span, "could not read cargo metadata"); + span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata"); return; };