From d055b7d61c44b7a401ebc470e3d1e3bd06a4d360 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 29 Mar 2020 12:59: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 +- tests/ui/replace_consts.fixed | 99 ----------------- tests/ui/replace_consts.rs | 99 ----------------- tests/ui/replace_consts.stderr | 152 --------------------------- 8 files changed, 14 insertions(+), 466 deletions(-) delete mode 100644 clippy_lints/src/replace_consts.rs delete mode 100644 tests/ui/replace_consts.fixed delete mode 100644 tests/ui/replace_consts.rs delete mode 100644 tests/ui/replace_consts.stderr 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", diff --git a/tests/ui/replace_consts.fixed b/tests/ui/replace_consts.fixed deleted file mode 100644 index 108474408e0f..000000000000 --- a/tests/ui/replace_consts.fixed +++ /dev/null @@ -1,99 +0,0 @@ -// run-rustfix -#![feature(integer_atomics)] -#![allow(unused_variables, clippy::blacklisted_name)] -#![deny(clippy::replace_consts)] - -use std::sync::atomic::*; - -#[rustfmt::skip] -fn bad() { - // Min - { let foo = isize::min_value(); }; - { let foo = i8::min_value(); }; - { let foo = i16::min_value(); }; - { let foo = i32::min_value(); }; - { let foo = i64::min_value(); }; - { let foo = i128::min_value(); }; - { let foo = usize::min_value(); }; - { let foo = u8::min_value(); }; - { let foo = u16::min_value(); }; - { let foo = u32::min_value(); }; - { let foo = u64::min_value(); }; - { let foo = u128::min_value(); }; - // Max - { let foo = isize::max_value(); }; - { let foo = i8::max_value(); }; - { let foo = i16::max_value(); }; - { let foo = i32::max_value(); }; - { let foo = i64::max_value(); }; - { let foo = i128::max_value(); }; - { let foo = usize::max_value(); }; - { let foo = u8::max_value(); }; - { let foo = u16::max_value(); }; - { let foo = u32::max_value(); }; - { let foo = u64::max_value(); }; - { let foo = u128::max_value(); }; -} - -#[rustfmt::skip] -fn good() { - // Atomic - { let foo = AtomicBool::new(false); }; - { let foo = AtomicIsize::new(0); }; - { let foo = AtomicI8::new(0); }; - { let foo = AtomicI16::new(0); }; - { let foo = AtomicI32::new(0); }; - { let foo = AtomicI64::new(0); }; - { let foo = AtomicUsize::new(0); }; - { let foo = AtomicU8::new(0); }; - { let foo = AtomicU16::new(0); }; - { let foo = AtomicU32::new(0); }; - { let foo = AtomicU64::new(0); }; - // Min - { let foo = isize::min_value(); }; - { let foo = i8::min_value(); }; - { let foo = i16::min_value(); }; - { let foo = i32::min_value(); }; - { let foo = i64::min_value(); }; - { let foo = i128::min_value(); }; - { let foo = usize::min_value(); }; - { let foo = u8::min_value(); }; - { let foo = u16::min_value(); }; - { let foo = u32::min_value(); }; - { let foo = u64::min_value(); }; - { let foo = u128::min_value(); }; - // Max - { let foo = isize::max_value(); }; - { let foo = i8::max_value(); }; - { let foo = i16::max_value(); }; - { let foo = i32::max_value(); }; - { let foo = i64::max_value(); }; - { let foo = i128::max_value(); }; - { let foo = usize::max_value(); }; - { let foo = u8::max_value(); }; - { let foo = u16::max_value(); }; - { let foo = u32::max_value(); }; - { let foo = u64::max_value(); }; - { let foo = u128::max_value(); }; - - let x = 42; - - let _ = match x { - std::i8::MIN => -1, - 1..=std::i8::MAX => 1, - _ => 0 - }; - - let _ = if let std::i8::MIN = x { - -1 - } else if let 1..=std::i8::MAX = x { - 1 - } else { - 0 - }; -} - -fn main() { - bad(); - good(); -} diff --git a/tests/ui/replace_consts.rs b/tests/ui/replace_consts.rs deleted file mode 100644 index dae3422a35f0..000000000000 --- a/tests/ui/replace_consts.rs +++ /dev/null @@ -1,99 +0,0 @@ -// run-rustfix -#![feature(integer_atomics)] -#![allow(unused_variables, clippy::blacklisted_name)] -#![deny(clippy::replace_consts)] - -use std::sync::atomic::*; - -#[rustfmt::skip] -fn bad() { - // Min - { let foo = std::isize::MIN; }; - { let foo = std::i8::MIN; }; - { let foo = std::i16::MIN; }; - { let foo = std::i32::MIN; }; - { let foo = std::i64::MIN; }; - { let foo = std::i128::MIN; }; - { let foo = std::usize::MIN; }; - { let foo = std::u8::MIN; }; - { let foo = std::u16::MIN; }; - { let foo = std::u32::MIN; }; - { let foo = std::u64::MIN; }; - { let foo = std::u128::MIN; }; - // Max - { let foo = std::isize::MAX; }; - { let foo = std::i8::MAX; }; - { let foo = std::i16::MAX; }; - { let foo = std::i32::MAX; }; - { let foo = std::i64::MAX; }; - { let foo = std::i128::MAX; }; - { let foo = std::usize::MAX; }; - { let foo = std::u8::MAX; }; - { let foo = std::u16::MAX; }; - { let foo = std::u32::MAX; }; - { let foo = std::u64::MAX; }; - { let foo = std::u128::MAX; }; -} - -#[rustfmt::skip] -fn good() { - // Atomic - { let foo = AtomicBool::new(false); }; - { let foo = AtomicIsize::new(0); }; - { let foo = AtomicI8::new(0); }; - { let foo = AtomicI16::new(0); }; - { let foo = AtomicI32::new(0); }; - { let foo = AtomicI64::new(0); }; - { let foo = AtomicUsize::new(0); }; - { let foo = AtomicU8::new(0); }; - { let foo = AtomicU16::new(0); }; - { let foo = AtomicU32::new(0); }; - { let foo = AtomicU64::new(0); }; - // Min - { let foo = isize::min_value(); }; - { let foo = i8::min_value(); }; - { let foo = i16::min_value(); }; - { let foo = i32::min_value(); }; - { let foo = i64::min_value(); }; - { let foo = i128::min_value(); }; - { let foo = usize::min_value(); }; - { let foo = u8::min_value(); }; - { let foo = u16::min_value(); }; - { let foo = u32::min_value(); }; - { let foo = u64::min_value(); }; - { let foo = u128::min_value(); }; - // Max - { let foo = isize::max_value(); }; - { let foo = i8::max_value(); }; - { let foo = i16::max_value(); }; - { let foo = i32::max_value(); }; - { let foo = i64::max_value(); }; - { let foo = i128::max_value(); }; - { let foo = usize::max_value(); }; - { let foo = u8::max_value(); }; - { let foo = u16::max_value(); }; - { let foo = u32::max_value(); }; - { let foo = u64::max_value(); }; - { let foo = u128::max_value(); }; - - let x = 42; - - let _ = match x { - std::i8::MIN => -1, - 1..=std::i8::MAX => 1, - _ => 0 - }; - - let _ = if let std::i8::MIN = x { - -1 - } else if let 1..=std::i8::MAX = x { - 1 - } else { - 0 - }; -} - -fn main() { - bad(); - good(); -} diff --git a/tests/ui/replace_consts.stderr b/tests/ui/replace_consts.stderr deleted file mode 100644 index 458f63953efa..000000000000 --- a/tests/ui/replace_consts.stderr +++ /dev/null @@ -1,152 +0,0 @@ -error: using `MIN` - --> $DIR/replace_consts.rs:11:17 - | -LL | { let foo = std::isize::MIN; }; - | ^^^^^^^^^^^^^^^ help: try this: `isize::min_value()` - | -note: the lint level is defined here - --> $DIR/replace_consts.rs:4:9 - | -LL | #![deny(clippy::replace_consts)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: using `MIN` - --> $DIR/replace_consts.rs:12:17 - | -LL | { let foo = std::i8::MIN; }; - | ^^^^^^^^^^^^ help: try this: `i8::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:13:17 - | -LL | { let foo = std::i16::MIN; }; - | ^^^^^^^^^^^^^ help: try this: `i16::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:14:17 - | -LL | { let foo = std::i32::MIN; }; - | ^^^^^^^^^^^^^ help: try this: `i32::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:15:17 - | -LL | { let foo = std::i64::MIN; }; - | ^^^^^^^^^^^^^ help: try this: `i64::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:16:17 - | -LL | { let foo = std::i128::MIN; }; - | ^^^^^^^^^^^^^^ help: try this: `i128::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:17:17 - | -LL | { let foo = std::usize::MIN; }; - | ^^^^^^^^^^^^^^^ help: try this: `usize::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:18:17 - | -LL | { let foo = std::u8::MIN; }; - | ^^^^^^^^^^^^ help: try this: `u8::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:19:17 - | -LL | { let foo = std::u16::MIN; }; - | ^^^^^^^^^^^^^ help: try this: `u16::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:20:17 - | -LL | { let foo = std::u32::MIN; }; - | ^^^^^^^^^^^^^ help: try this: `u32::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:21:17 - | -LL | { let foo = std::u64::MIN; }; - | ^^^^^^^^^^^^^ help: try this: `u64::min_value()` - -error: using `MIN` - --> $DIR/replace_consts.rs:22:17 - | -LL | { let foo = std::u128::MIN; }; - | ^^^^^^^^^^^^^^ help: try this: `u128::min_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:24:17 - | -LL | { let foo = std::isize::MAX; }; - | ^^^^^^^^^^^^^^^ help: try this: `isize::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:25:17 - | -LL | { let foo = std::i8::MAX; }; - | ^^^^^^^^^^^^ help: try this: `i8::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:26:17 - | -LL | { let foo = std::i16::MAX; }; - | ^^^^^^^^^^^^^ help: try this: `i16::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:27:17 - | -LL | { let foo = std::i32::MAX; }; - | ^^^^^^^^^^^^^ help: try this: `i32::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:28:17 - | -LL | { let foo = std::i64::MAX; }; - | ^^^^^^^^^^^^^ help: try this: `i64::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:29:17 - | -LL | { let foo = std::i128::MAX; }; - | ^^^^^^^^^^^^^^ help: try this: `i128::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:30:17 - | -LL | { let foo = std::usize::MAX; }; - | ^^^^^^^^^^^^^^^ help: try this: `usize::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:31:17 - | -LL | { let foo = std::u8::MAX; }; - | ^^^^^^^^^^^^ help: try this: `u8::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:32:17 - | -LL | { let foo = std::u16::MAX; }; - | ^^^^^^^^^^^^^ help: try this: `u16::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:33:17 - | -LL | { let foo = std::u32::MAX; }; - | ^^^^^^^^^^^^^ help: try this: `u32::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:34:17 - | -LL | { let foo = std::u64::MAX; }; - | ^^^^^^^^^^^^^ help: try this: `u64::max_value()` - -error: using `MAX` - --> $DIR/replace_consts.rs:35:17 - | -LL | { let foo = std::u128::MAX; }; - | ^^^^^^^^^^^^^^ help: try this: `u128::max_value()` - -error: aborting due to 24 previous errors -