Auto merge of #5380 - lzutao:deprecate-REPLACE_CONSTS, r=phansch

Deprecate REPLACE_CONSTS lint

Closes #5346
changelog: Deprecate `replace_consts` lint
This commit is contained in:
bors 2020-03-30 08:03:42 +00:00
commit bfd11235a6
8 changed files with 14 additions and 466 deletions

View file

@ -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:

View file

@ -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"
}

View file

@ -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, its used in `update_lints`
// begin register lints, do not remove this comment, its used in `update_lints`
@ -755,7 +758,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&regex::INVALID_REGEX,
&regex::REGEX_MACRO,
&regex::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),

View file

@ -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()"),
];

View file

@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;
// begin lint list, do not remove this comment, its 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",

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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