From 7e35a7465895ead6eab1c78d4925e929bc620f22 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 29 Mar 2020 12:50:35 +0700 Subject: [PATCH] Deprecate REPLACE_CONSTS lint --- README.md | 2 +- clippy_lints/src/deprecated_lints.rs | 8 +++ clippy_lints/src/lib.rs | 8 +-- clippy_lints/src/replace_consts.rs | 103 --------------------------- src/lintlist/mod.rs | 9 +-- 5 files changed, 14 insertions(+), 116 deletions(-) delete mode 100644 clippy_lints/src/replace_consts.rs diff --git a/README.md b/README.md index ebdab4c6a1a0..5733994a06c4 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 362 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are 361 lints included in this crate!](https://rust-lang.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: diff --git a/clippy_lints/src/deprecated_lints.rs b/clippy_lints/src/deprecated_lints.rs index 93c19bf95506..6e8ca647dd7a 100644 --- a/clippy_lints/src/deprecated_lints.rs +++ b/clippy_lints/src/deprecated_lints.rs @@ -147,3 +147,11 @@ declare_deprecated_lint! { pub UNUSED_LABEL, "this lint has been uplifted to rustc and is now called `unused_labels`" } + +declare_deprecated_lint! { + /// **What it does:** Nothing. This lint has been deprecated. + /// + /// **Deprecation reason:** Associated-constants are now preferred. + pub REPLACE_CONSTS, + "associated-constants `MIN`/`MAX` of integers are prefer to `{min,max}_value()` and module constants" +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 140500afe0f9..704b46a87c91 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -291,7 +291,6 @@ pub mod redundant_pub_crate; pub mod redundant_static_lifetimes; pub mod reference; pub mod regex; -pub mod replace_consts; pub mod returns; pub mod serde_api; pub mod shadow; @@ -470,6 +469,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: "clippy::unused_label", "this lint has been uplifted to rustc and is now called `unused_labels`", ); + store.register_removed( + "clippy::replace_consts", + "associated-constants `MIN`/`MAX` of integers are prefer to `{min,max}_value()` and module constants", + ); // end deprecated lints, do not remove this comment, it’s used in `update_lints` // begin register lints, do not remove this comment, it’s used in `update_lints` @@ -755,7 +758,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: ®ex::INVALID_REGEX, ®ex::REGEX_MACRO, ®ex::TRIVIAL_REGEX, - &replace_consts::REPLACE_CONSTS, &returns::LET_AND_RETURN, &returns::NEEDLESS_RETURN, &returns::UNUSED_UNIT, @@ -953,7 +955,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|| box identity_conversion::IdentityConversion::default()); store.register_late_pass(|| box types::ImplicitHasher); store.register_late_pass(|| box fallible_impl_from::FallibleImplFrom); - store.register_late_pass(|| box replace_consts::ReplaceConsts); store.register_late_pass(|| box types::UnitArg); store.register_late_pass(|| box double_comparison::DoubleComparisons); store.register_late_pass(|| box question_mark::QuestionMark); @@ -1110,7 +1111,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&needless_pass_by_value::NEEDLESS_PASS_BY_VALUE), LintId::of(&non_expressive_names::SIMILAR_NAMES), LintId::of(&ranges::RANGE_PLUS_ONE), - LintId::of(&replace_consts::REPLACE_CONSTS), LintId::of(&shadow::SHADOW_UNRELATED), LintId::of(&strings::STRING_ADD_ASSIGN), LintId::of(&trait_bounds::TYPE_REPETITION_IN_BOUNDS), diff --git a/clippy_lints/src/replace_consts.rs b/clippy_lints/src/replace_consts.rs deleted file mode 100644 index aca1ebbd5080..000000000000 --- a/clippy_lints/src/replace_consts.rs +++ /dev/null @@ -1,103 +0,0 @@ -use crate::utils::{match_def_path, span_lint_and_sugg}; -use if_chain::if_chain; -use rustc_errors::Applicability; -use rustc_hir::def::{DefKind, Res}; -use rustc_hir::{Expr, ExprKind, Node}; -use rustc_lint::{LateContext, LateLintPass}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; - -declare_clippy_lint! { - /// **What it does:** Checks for usage of standard library - /// `const`s that could be replaced by `const fn`s. - /// - /// **Why is this bad?** `const fn`s exist - /// - /// **Known problems:** None. - /// - /// **Example:** - /// ```rust - /// let x = std::u32::MIN; - /// let y = std::u32::MAX; - /// ``` - /// - /// Could be written: - /// - /// ```rust - /// let x = u32::min_value(); - /// let y = u32::max_value(); - /// ``` - pub REPLACE_CONSTS, - pedantic, - "Lint usages of standard library `const`s that could be replaced by `const fn`s" -} - -declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]); - -fn in_pattern(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { - let map = &cx.tcx.hir(); - let parent_id = map.get_parent_node(expr.hir_id); - - if let Some(node) = map.find(parent_id) { - if let Node::Pat(_) = node { - return true; - } - } - - false -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts { - fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { - if_chain! { - if let ExprKind::Path(ref qp) = expr.kind; - if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id); - // Do not lint within patterns as function calls are disallowed in them - if !in_pattern(cx, expr); - then { - for &(ref const_path, repl_snip) in &REPLACEMENTS { - if match_def_path(cx, def_id, const_path) { - span_lint_and_sugg( - cx, - REPLACE_CONSTS, - expr.span, - &format!("using `{}`", const_path.last().expect("empty path")), - "try this", - repl_snip.to_string(), - Applicability::MachineApplicable, - ); - return; - } - } - } - } - } -} - -const REPLACEMENTS: [([&str; 3], &str); 24] = [ - // Min - (["core", "isize", "MIN"], "isize::min_value()"), - (["core", "i8", "MIN"], "i8::min_value()"), - (["core", "i16", "MIN"], "i16::min_value()"), - (["core", "i32", "MIN"], "i32::min_value()"), - (["core", "i64", "MIN"], "i64::min_value()"), - (["core", "i128", "MIN"], "i128::min_value()"), - (["core", "usize", "MIN"], "usize::min_value()"), - (["core", "u8", "MIN"], "u8::min_value()"), - (["core", "u16", "MIN"], "u16::min_value()"), - (["core", "u32", "MIN"], "u32::min_value()"), - (["core", "u64", "MIN"], "u64::min_value()"), - (["core", "u128", "MIN"], "u128::min_value()"), - // Max - (["core", "isize", "MAX"], "isize::max_value()"), - (["core", "i8", "MAX"], "i8::max_value()"), - (["core", "i16", "MAX"], "i16::max_value()"), - (["core", "i32", "MAX"], "i32::max_value()"), - (["core", "i64", "MAX"], "i64::max_value()"), - (["core", "i128", "MAX"], "i128::max_value()"), - (["core", "usize", "MAX"], "usize::max_value()"), - (["core", "u8", "MAX"], "u8::max_value()"), - (["core", "u16", "MAX"], "u16::max_value()"), - (["core", "u32", "MAX"], "u32::max_value()"), - (["core", "u64", "MAX"], "u64::max_value()"), - (["core", "u128", "MAX"], "u128::max_value()"), -]; diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index 2888741cb454..5777e7d90e58 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -6,7 +6,7 @@ pub use lint::Lint; pub use lint::LINT_LEVELS; // begin lint list, do not remove this comment, it’s used in `update_lints` -pub const ALL_LINTS: [Lint; 362] = [ +pub const ALL_LINTS: [Lint; 361] = [ Lint { name: "absurd_extreme_comparisons", group: "correctness", @@ -1792,13 +1792,6 @@ pub const ALL_LINTS: [Lint; 362] = [ deprecation: None, module: "regex", }, - Lint { - name: "replace_consts", - group: "pedantic", - desc: "Lint usages of standard library `const`s that could be replaced by `const fn`s", - deprecation: None, - module: "replace_consts", - }, Lint { name: "rest_pat_in_fully_bound_structs", group: "restriction",