Skip to content

Commit 0c392d9

Browse files
Centri3pitaj
authored andcommittedMar 21, 2024·
new lint legacy_numeric_constants
1 parent 9d6f416 commit 0c392d9

22 files changed

+921
-37
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5379,6 +5379,7 @@ Released 2018-09-13
53795379
[`large_stack_arrays`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_stack_arrays
53805380
[`large_stack_frames`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_stack_frames
53815381
[`large_types_passed_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_types_passed_by_value
5382+
[`legacy_numeric_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#legacy_numeric_constants
53825383
[`len_without_is_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty
53835384
[`len_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
53845385
[`let_and_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return

‎book/src/lint_configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
616616
* [`if_then_some_else_none`](https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none)
617617
* [`index_refutable_slice`](https://rust-lang.github.io/rust-clippy/master/index.html#index_refutable_slice)
618618
* [`iter_kv_map`](https://rust-lang.github.io/rust-clippy/master/index.html#iter_kv_map)
619+
* [`legacy_numeric_constants`](https://rust-lang.github.io/rust-clippy/master/index.html#legacy_numeric_constants)
619620
* [`manual_bits`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_bits)
620621
* [`manual_c_str_literals`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals)
621622
* [`manual_clamp`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_clamp)

‎clippy_config/src/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ define_Conf! {
262262
///
263263
/// Suppress lints whenever the suggested change would cause breakage for other crates.
264264
(avoid_breaking_exported_api: bool = true),
265-
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, OPTION_MAP_UNWRAP_OR, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP, MANUAL_LET_ELSE, UNCHECKED_DURATION_SUBTRACTION, COLLAPSIBLE_STR_REPLACE, SEEK_FROM_CURRENT, SEEK_REWIND, UNNECESSARY_LAZY_EVALUATIONS, TRANSMUTE_PTR_TO_REF, ALMOST_COMPLETE_RANGE, NEEDLESS_BORROW, DERIVABLE_IMPLS, MANUAL_IS_ASCII_CHECK, MANUAL_REM_EUCLID, MANUAL_RETAIN, TYPE_REPETITION_IN_BOUNDS, TUPLE_ARRAY_CONVERSIONS, MANUAL_TRY_FOLD, MANUAL_HASH_ONE, ITER_KV_MAP, MANUAL_C_STR_LITERALS, ASSIGNING_CLONES.
265+
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, OPTION_MAP_UNWRAP_OR, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP, MANUAL_LET_ELSE, UNCHECKED_DURATION_SUBTRACTION, COLLAPSIBLE_STR_REPLACE, SEEK_FROM_CURRENT, SEEK_REWIND, UNNECESSARY_LAZY_EVALUATIONS, TRANSMUTE_PTR_TO_REF, ALMOST_COMPLETE_RANGE, NEEDLESS_BORROW, DERIVABLE_IMPLS, MANUAL_IS_ASCII_CHECK, MANUAL_REM_EUCLID, MANUAL_RETAIN, TYPE_REPETITION_IN_BOUNDS, TUPLE_ARRAY_CONVERSIONS, MANUAL_TRY_FOLD, MANUAL_HASH_ONE, ITER_KV_MAP, MANUAL_C_STR_LITERALS, ASSIGNING_CLONES, LEGACY_NUMERIC_CONSTANTS.
266266
///
267267
/// The minimum rust version that the project supports. Defaults to the `rust-version` field in `Cargo.toml`
268268
#[default_text = ""]

‎clippy_config/src/msrvs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ msrv_aliases! {
3636
1,47,0 { TAU, IS_ASCII_DIGIT_CONST, ARRAY_IMPL_ANY_LEN }
3737
1,46,0 { CONST_IF_MATCH }
3838
1,45,0 { STR_STRIP_PREFIX }
39-
1,43,0 { LOG2_10, LOG10_2 }
39+
1,43,0 { LOG2_10, LOG10_2, NUMERIC_ASSOCIATED_CONSTANTS }
4040
1,42,0 { MATCHES_MACRO, SLICE_PATTERNS, PTR_SLICE_RAW_PARTS }
4141
1,41,0 { RE_REBALANCING_COHERENCE, RESULT_MAP_OR_ELSE }
4242
1,40,0 { MEM_TAKE, NON_EXHAUSTIVE, OPTION_AS_DEREF }

‎clippy_lints/src/casts/cast_possible_truncation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
4141
})
4242
},
4343
BinOpKind::Rem | BinOpKind::BitAnd => get_constant_bits(cx, right)
44-
.unwrap_or(u64::max_value())
44+
.unwrap_or(u64::MAX)
4545
.min(apply_reductions(cx, nbits, left, signed)),
4646
BinOpKind::Shr => apply_reductions(cx, nbits, left, signed)
4747
.saturating_sub(constant_int(cx, right).map_or(0, |s| u64::try_from(s).unwrap_or_default())),
@@ -56,7 +56,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
5656
} else {
5757
None
5858
};
59-
apply_reductions(cx, nbits, left, signed).min(max_bits.unwrap_or(u64::max_value()))
59+
apply_reductions(cx, nbits, left, signed).min(max_bits.unwrap_or(u64::MAX))
6060
},
6161
ExprKind::MethodCall(method, _, [lo, hi], _) => {
6262
if method.ident.as_str() == "clamp" {

‎clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
254254
crate::large_include_file::LARGE_INCLUDE_FILE_INFO,
255255
crate::large_stack_arrays::LARGE_STACK_ARRAYS_INFO,
256256
crate::large_stack_frames::LARGE_STACK_FRAMES_INFO,
257+
crate::legacy_numeric_constants::LEGACY_NUMERIC_CONSTANTS_INFO,
257258
crate::len_zero::COMPARISON_TO_EMPTY_INFO,
258259
crate::len_zero::LEN_WITHOUT_IS_EMPTY_INFO,
259260
crate::len_zero::LEN_ZERO_INFO,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
use clippy_config::msrvs::{Msrv, NUMERIC_ASSOCIATED_CONSTANTS};
2+
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
3+
use clippy_utils::{get_parent_expr, is_from_proc_macro};
4+
use hir::def_id::DefId;
5+
use rustc_errors::{Applicability, SuggestionStyle};
6+
use rustc_hir as hir;
7+
use rustc_hir::{ExprKind, Item, ItemKind, QPath, UseKind};
8+
use rustc_lint::{LateContext, LateLintPass, LintContext};
9+
use rustc_middle::lint::in_external_macro;
10+
use rustc_session::impl_lint_pass;
11+
use rustc_span::symbol::kw;
12+
use rustc_span::{sym, Symbol};
13+
14+
declare_clippy_lint! {
15+
/// ### What it does
16+
/// Checks for usage of `<integer>::max_value()`, `std::<integer>::MAX`,
17+
/// `std::<float>::EPSILON`, etc.
18+
///
19+
/// ### Why is this bad?
20+
/// All of these have been superceded by the associated constants on their respective types,
21+
/// such as `i128::MAX`. These legacy items may be deprecated in a future version of rust.
22+
///
23+
/// ### Example
24+
/// ```rust
25+
/// let eps = std::f32::EPSILON;
26+
/// ```
27+
/// Use instead:
28+
/// ```rust
29+
/// let eps = f32::EPSILON;
30+
/// ```
31+
#[clippy::version = "1.72.0"]
32+
pub LEGACY_NUMERIC_CONSTANTS,
33+
style,
34+
"checks for usage of legacy std numeric constants and methods"
35+
}
36+
pub struct LegacyNumericConstants {
37+
msrv: Msrv,
38+
}
39+
40+
impl LegacyNumericConstants {
41+
#[must_use]
42+
pub fn new(msrv: Msrv) -> Self {
43+
Self { msrv }
44+
}
45+
}
46+
47+
impl_lint_pass!(LegacyNumericConstants => [LEGACY_NUMERIC_CONSTANTS]);
48+
49+
impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
50+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
51+
let Self { msrv } = self;
52+
53+
if !msrv.meets(NUMERIC_ASSOCIATED_CONSTANTS) || in_external_macro(cx.sess(), item.span) {
54+
return;
55+
}
56+
57+
// Integer modules are "TBD" deprecated, and the contents are too,
58+
// so lint on the `use` statement directly.
59+
if let ItemKind::Use(path, kind @ (UseKind::Single | UseKind::Glob)) = item.kind
60+
&& let Some(def_id) = path.res[0].opt_def_id()
61+
{
62+
let module = if is_integer_module(cx, def_id) {
63+
true
64+
} else if is_numeric_const(cx, def_id) {
65+
false
66+
} else {
67+
return;
68+
};
69+
70+
span_lint_and_then(
71+
cx,
72+
LEGACY_NUMERIC_CONSTANTS,
73+
path.span,
74+
if module {
75+
"importing legacy numeric constants"
76+
} else {
77+
"importing a legacy numeric constant"
78+
},
79+
|diag| {
80+
if item.ident.name == kw::Underscore {
81+
diag.help("remove this import");
82+
return;
83+
}
84+
85+
let def_path = cx.get_def_path(def_id);
86+
87+
if module && let [.., module_name] = &*def_path {
88+
if kind == UseKind::Glob {
89+
diag.help(format!("remove this import and use associated constants `{module_name}::<CONST>` from the primitive type instead"));
90+
} else {
91+
diag.help("remove this import").note(format!(
92+
"then `{module_name}::<CONST>` will resolve to the respective associated constant"
93+
));
94+
}
95+
} else if let [.., module_name, name] = &*def_path {
96+
diag.help(
97+
format!("remove this import and use the associated constant `{module_name}::{name}` from the primitive type instead")
98+
);
99+
}
100+
},
101+
);
102+
}
103+
}
104+
105+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
106+
let Self { msrv } = self;
107+
108+
if !msrv.meets(NUMERIC_ASSOCIATED_CONSTANTS) || in_external_macro(cx.sess(), expr.span) {
109+
return;
110+
}
111+
let ExprKind::Path(qpath) = expr.kind else {
112+
return;
113+
};
114+
115+
// `std::<integer>::<CONST>` check
116+
let (span, sugg, msg) = if let QPath::Resolved(None, path) = qpath
117+
&& let Some(def_id) = path.res.opt_def_id()
118+
&& is_numeric_const(cx, def_id)
119+
&& let def_path = cx.get_def_path(def_id)
120+
&& let [.., mod_name, name] = &*def_path
121+
// Skip linting if this usage looks identical to the associated constant,
122+
// since this would only require removing a `use` import (which is already linted).
123+
&& !is_numeric_const_path_canonical(path, [*mod_name, *name])
124+
{
125+
(
126+
expr.span,
127+
format!("{mod_name}::{name}"),
128+
"usage of a legacy numeric constant",
129+
)
130+
// `<integer>::xxx_value` check
131+
} else if let QPath::TypeRelative(_, last_segment) = qpath
132+
&& let Some(def_id) = cx.qpath_res(&qpath, expr.hir_id).opt_def_id()
133+
&& is_integer_method(cx, def_id)
134+
&& let Some(par_expr) = get_parent_expr(cx, expr)
135+
&& let ExprKind::Call(_, _) = par_expr.kind
136+
{
137+
let name = last_segment.ident.name.as_str();
138+
139+
(
140+
last_segment.ident.span.with_hi(par_expr.span.hi()),
141+
name[..=2].to_ascii_uppercase(),
142+
"usage of a legacy numeric method",
143+
)
144+
} else {
145+
return;
146+
};
147+
148+
if is_from_proc_macro(cx, expr) {
149+
return;
150+
}
151+
152+
span_lint_hir_and_then(cx, LEGACY_NUMERIC_CONSTANTS, expr.hir_id, span, msg, |diag| {
153+
diag.span_suggestion_with_style(
154+
span,
155+
"use the associated constant instead",
156+
sugg,
157+
Applicability::MaybeIncorrect,
158+
SuggestionStyle::ShowAlways,
159+
);
160+
});
161+
}
162+
163+
extract_msrv_attr!(LateContext);
164+
}
165+
166+
fn is_integer_module(cx: &LateContext<'_>, did: DefId) -> bool {
167+
[
168+
sym::isize_legacy_mod,
169+
sym::i128_legacy_mod,
170+
sym::i64_legacy_mod,
171+
sym::i32_legacy_mod,
172+
sym::i16_legacy_mod,
173+
sym::i8_legacy_mod,
174+
sym::usize_legacy_mod,
175+
sym::u128_legacy_mod,
176+
sym::u64_legacy_mod,
177+
sym::u32_legacy_mod,
178+
sym::u16_legacy_mod,
179+
sym::u8_legacy_mod,
180+
]
181+
.iter()
182+
.any(|&name| cx.tcx.is_diagnostic_item(name, did))
183+
}
184+
185+
fn is_numeric_const(cx: &LateContext<'_>, did: DefId) -> bool {
186+
[
187+
sym::isize_legacy_const_max,
188+
sym::isize_legacy_const_min,
189+
sym::i128_legacy_const_max,
190+
sym::i128_legacy_const_min,
191+
sym::i16_legacy_const_max,
192+
sym::i16_legacy_const_min,
193+
sym::i32_legacy_const_max,
194+
sym::i32_legacy_const_min,
195+
sym::i64_legacy_const_max,
196+
sym::i64_legacy_const_min,
197+
sym::i8_legacy_const_max,
198+
sym::i8_legacy_const_min,
199+
sym::usize_legacy_const_max,
200+
sym::usize_legacy_const_min,
201+
sym::u128_legacy_const_max,
202+
sym::u128_legacy_const_min,
203+
sym::u16_legacy_const_max,
204+
sym::u16_legacy_const_min,
205+
sym::u32_legacy_const_max,
206+
sym::u32_legacy_const_min,
207+
sym::u64_legacy_const_max,
208+
sym::u64_legacy_const_min,
209+
sym::u8_legacy_const_max,
210+
sym::u8_legacy_const_min,
211+
sym::f32_legacy_const_digits,
212+
sym::f32_legacy_const_epsilon,
213+
sym::f32_legacy_const_infinity,
214+
sym::f32_legacy_const_mantissa_dig,
215+
sym::f32_legacy_const_max,
216+
sym::f32_legacy_const_max_10_exp,
217+
sym::f32_legacy_const_max_exp,
218+
sym::f32_legacy_const_min,
219+
sym::f32_legacy_const_min_10_exp,
220+
sym::f32_legacy_const_min_exp,
221+
sym::f32_legacy_const_min_positive,
222+
sym::f32_legacy_const_nan,
223+
sym::f32_legacy_const_neg_infinity,
224+
sym::f32_legacy_const_radix,
225+
sym::f64_legacy_const_digits,
226+
sym::f64_legacy_const_epsilon,
227+
sym::f64_legacy_const_infinity,
228+
sym::f64_legacy_const_mantissa_dig,
229+
sym::f64_legacy_const_max,
230+
sym::f64_legacy_const_max_10_exp,
231+
sym::f64_legacy_const_max_exp,
232+
sym::f64_legacy_const_min,
233+
sym::f64_legacy_const_min_10_exp,
234+
sym::f64_legacy_const_min_exp,
235+
sym::f64_legacy_const_min_positive,
236+
sym::f64_legacy_const_nan,
237+
sym::f64_legacy_const_neg_infinity,
238+
sym::f64_legacy_const_radix,
239+
]
240+
.iter()
241+
.any(|&name| cx.tcx.is_diagnostic_item(name, did))
242+
}
243+
244+
// Whether path expression looks like `i32::MAX`
245+
fn is_numeric_const_path_canonical(expr_path: &hir::Path<'_>, [mod_name, name]: [Symbol; 2]) -> bool {
246+
let [
247+
hir::PathSegment {
248+
ident: one, args: None, ..
249+
},
250+
hir::PathSegment {
251+
ident: two, args: None, ..
252+
},
253+
] = expr_path.segments
254+
else {
255+
return false;
256+
};
257+
258+
one.name == mod_name && two.name == name
259+
}
260+
261+
fn is_integer_method(cx: &LateContext<'_>, did: DefId) -> bool {
262+
[
263+
sym::isize_legacy_fn_max_value,
264+
sym::isize_legacy_fn_min_value,
265+
sym::i128_legacy_fn_max_value,
266+
sym::i128_legacy_fn_min_value,
267+
sym::i16_legacy_fn_max_value,
268+
sym::i16_legacy_fn_min_value,
269+
sym::i32_legacy_fn_max_value,
270+
sym::i32_legacy_fn_min_value,
271+
sym::i64_legacy_fn_max_value,
272+
sym::i64_legacy_fn_min_value,
273+
sym::i8_legacy_fn_max_value,
274+
sym::i8_legacy_fn_min_value,
275+
sym::usize_legacy_fn_max_value,
276+
sym::usize_legacy_fn_min_value,
277+
sym::u128_legacy_fn_max_value,
278+
sym::u128_legacy_fn_min_value,
279+
sym::u16_legacy_fn_max_value,
280+
sym::u16_legacy_fn_min_value,
281+
sym::u32_legacy_fn_max_value,
282+
sym::u32_legacy_fn_min_value,
283+
sym::u64_legacy_fn_max_value,
284+
sym::u64_legacy_fn_min_value,
285+
sym::u8_legacy_fn_max_value,
286+
sym::u8_legacy_fn_min_value,
287+
]
288+
.iter()
289+
.any(|&name| cx.tcx.is_diagnostic_item(name, did))
290+
}

‎clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ mod large_futures;
186186
mod large_include_file;
187187
mod large_stack_arrays;
188188
mod large_stack_frames;
189+
mod legacy_numeric_constants;
189190
mod len_zero;
190191
mod let_if_seq;
191192
mod let_underscore;
@@ -1080,6 +1081,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
10801081
allow_one_hash_in_raw_strings,
10811082
})
10821083
});
1084+
store.register_late_pass(move |_| Box::new(legacy_numeric_constants::LegacyNumericConstants::new(msrv())));
10831085
store.register_late_pass(|_| Box::new(manual_range_patterns::ManualRangePatterns));
10841086
store.register_early_pass(|| Box::new(visibility::Visibility));
10851087
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions { msrv: msrv() }));

‎clippy_utils/src/check_proc_macro.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_hir::{
2424
use rustc_lint::{LateContext, LintContext};
2525
use rustc_middle::ty::TyCtxt;
2626
use rustc_session::Session;
27-
use rustc_span::symbol::Ident;
27+
use rustc_span::symbol::{kw, Ident};
2828
use rustc_span::{Span, Symbol};
2929
use rustc_target::spec::abi::Abi;
3030

@@ -99,9 +99,13 @@ fn qpath_search_pat(path: &QPath<'_>) -> (Pat, Pat) {
9999
let start = if ty.is_some() {
100100
Pat::Str("<")
101101
} else {
102-
path.segments
103-
.first()
104-
.map_or(Pat::Str(""), |seg| Pat::Sym(seg.ident.name))
102+
path.segments.first().map_or(Pat::Str(""), |seg| {
103+
if seg.ident.name == kw::PathRoot {
104+
Pat::Str("::")
105+
} else {
106+
Pat::Sym(seg.ident.name)
107+
}
108+
})
105109
};
106110
let end = path.segments.last().map_or(Pat::Str(""), |seg| {
107111
if seg.args.is_some() {

‎tests/ui-toml/absolute_paths/absolute_paths.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@revisions: allow_crates disallow_crates
44
//@[allow_crates] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/absolute_paths/allow_crates
55
//@[disallow_crates] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/absolute_paths/disallow_crates
6-
#![allow(clippy::no_effect, unused)]
6+
#![allow(clippy::no_effect, clippy::legacy_numeric_constants, unused)]
77
#![warn(clippy::absolute_paths)]
88
#![feature(decl_macro)]
99

‎tests/ui/checked_conversions.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(
22
clippy::cast_lossless,
3+
clippy::legacy_numeric_constants,
34
unused,
45
// Int::max_value will be deprecated in the future
56
deprecated,

‎tests/ui/checked_conversions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(
22
clippy::cast_lossless,
3+
clippy::legacy_numeric_constants,
34
unused,
45
// Int::max_value will be deprecated in the future
56
deprecated,

‎tests/ui/checked_conversions.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: checked cast can be simplified
2-
--> tests/ui/checked_conversions.rs:14:13
2+
--> tests/ui/checked_conversions.rs:15:13
33
|
44
LL | let _ = value <= (u32::max_value() as i64) && value >= 0;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
@@ -8,97 +8,97 @@ LL | let _ = value <= (u32::max_value() as i64) && value >= 0;
88
= help: to override `-D warnings` add `#[allow(clippy::checked_conversions)]`
99

1010
error: checked cast can be simplified
11-
--> tests/ui/checked_conversions.rs:15:13
11+
--> tests/ui/checked_conversions.rs:16:13
1212
|
1313
LL | let _ = value <= (u32::MAX as i64) && value >= 0;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
1515

1616
error: checked cast can be simplified
17-
--> tests/ui/checked_conversions.rs:19:13
17+
--> tests/ui/checked_conversions.rs:20:13
1818
|
1919
LL | let _ = value <= i64::from(u16::max_value()) && value >= 0;
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
2121

2222
error: checked cast can be simplified
23-
--> tests/ui/checked_conversions.rs:20:13
23+
--> tests/ui/checked_conversions.rs:21:13
2424
|
2525
LL | let _ = value <= i64::from(u16::MAX) && value >= 0;
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
2727

2828
error: checked cast can be simplified
29-
--> tests/ui/checked_conversions.rs:24:13
29+
--> tests/ui/checked_conversions.rs:25:13
3030
|
3131
LL | let _ = value <= (u8::max_value() as isize) && value >= 0;
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
3333

3434
error: checked cast can be simplified
35-
--> tests/ui/checked_conversions.rs:25:13
35+
--> tests/ui/checked_conversions.rs:26:13
3636
|
3737
LL | let _ = value <= (u8::MAX as isize) && value >= 0;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
3939

4040
error: checked cast can be simplified
41-
--> tests/ui/checked_conversions.rs:31:13
41+
--> tests/ui/checked_conversions.rs:32:13
4242
|
4343
LL | let _ = value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64);
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
4545

4646
error: checked cast can be simplified
47-
--> tests/ui/checked_conversions.rs:32:13
47+
--> tests/ui/checked_conversions.rs:33:13
4848
|
4949
LL | let _ = value <= (i32::MAX as i64) && value >= (i32::MIN as i64);
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
5151

5252
error: checked cast can be simplified
53-
--> tests/ui/checked_conversions.rs:36:13
53+
--> tests/ui/checked_conversions.rs:37:13
5454
|
5555
LL | let _ = value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value());
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
5757

5858
error: checked cast can be simplified
59-
--> tests/ui/checked_conversions.rs:37:13
59+
--> tests/ui/checked_conversions.rs:38:13
6060
|
6161
LL | let _ = value <= i64::from(i16::MAX) && value >= i64::from(i16::MIN);
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
6363

6464
error: checked cast can be simplified
65-
--> tests/ui/checked_conversions.rs:43:13
65+
--> tests/ui/checked_conversions.rs:44:13
6666
|
6767
LL | let _ = value <= i32::max_value() as u32;
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
6969

7070
error: checked cast can be simplified
71-
--> tests/ui/checked_conversions.rs:44:13
71+
--> tests/ui/checked_conversions.rs:45:13
7272
|
7373
LL | let _ = value <= i32::MAX as u32;
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
7575

7676
error: checked cast can be simplified
77-
--> tests/ui/checked_conversions.rs:48:13
77+
--> tests/ui/checked_conversions.rs:49:13
7878
|
7979
LL | let _ = value <= isize::max_value() as usize && value as i32 == 5;
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
8181

8282
error: checked cast can be simplified
83-
--> tests/ui/checked_conversions.rs:49:13
83+
--> tests/ui/checked_conversions.rs:50:13
8484
|
8585
LL | let _ = value <= isize::MAX as usize && value as i32 == 5;
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
8787

8888
error: checked cast can be simplified
89-
--> tests/ui/checked_conversions.rs:53:13
89+
--> tests/ui/checked_conversions.rs:54:13
9090
|
9191
LL | let _ = value <= u16::max_value() as u32 && value as i32 == 5;
9292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
9393

9494
error: checked cast can be simplified
95-
--> tests/ui/checked_conversions.rs:54:13
95+
--> tests/ui/checked_conversions.rs:55:13
9696
|
9797
LL | let _ = value <= u16::MAX as u32 && value as i32 == 5;
9898
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
9999

100100
error: checked cast can be simplified
101-
--> tests/ui/checked_conversions.rs:87:13
101+
--> tests/ui/checked_conversions.rs:88:13
102102
|
103103
LL | let _ = value <= (u32::MAX as i64) && value >= 0;
104104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//@aux-build:proc_macros.rs
2+
#![allow(clippy::no_effect, deprecated, unused)]
3+
#![allow(clippy::legacy_numeric_constants)] // For imports.
4+
#![feature(lint_reasons)]
5+
6+
#[macro_use]
7+
extern crate proc_macros;
8+
9+
pub mod a {
10+
pub use std::u128;
11+
}
12+
13+
macro_rules! b {
14+
() => {
15+
mod b {
16+
#[warn(clippy::legacy_numeric_constants)]
17+
fn b() {
18+
let x = u64::MAX;
19+
//~^ ERROR: usage of a legacy numeric constant
20+
//~| HELP: use the associated constant instead
21+
}
22+
}
23+
};
24+
}
25+
26+
use std::u32::MAX;
27+
use std::u8::MIN;
28+
use std::{f64, u32};
29+
30+
#[warn(clippy::legacy_numeric_constants)]
31+
fn main() {
32+
f32::EPSILON;
33+
//~^ ERROR: usage of a legacy numeric constant
34+
//~| HELP: use the associated constant instead
35+
u8::MIN;
36+
//~^ ERROR: usage of a legacy numeric constant
37+
//~| HELP: use the associated constant instead
38+
usize::MIN;
39+
//~^ ERROR: usage of a legacy numeric constant
40+
//~| HELP: use the associated constant instead
41+
u32::MAX;
42+
//~^ ERROR: usage of a legacy numeric constant
43+
//~| HELP: use the associated constant instead
44+
u32::MAX;
45+
//~^ ERROR: usage of a legacy numeric constant
46+
//~| HELP: use the associated constant instead
47+
u32::MAX;
48+
//~^ ERROR: usage of a legacy numeric constant
49+
//~| HELP: use the associated constant instead
50+
i32::MAX;
51+
//~^ ERROR: usage of a legacy numeric method
52+
//~| HELP: use the associated constant instead
53+
u8::MAX;
54+
//~^ ERROR: usage of a legacy numeric method
55+
//~| HELP: use the associated constant instead
56+
u8::MIN;
57+
//~^ ERROR: usage of a legacy numeric method
58+
//~| HELP: use the associated constant instead
59+
u8::MIN;
60+
//~^ ERROR: usage of a legacy numeric constant
61+
//~| HELP: use the associated constant instead
62+
::std::primitive::u8::MIN;
63+
//~^ ERROR: usage of a legacy numeric method
64+
//~| HELP: use the associated constant instead
65+
std::primitive::i32::MAX;
66+
//~^ ERROR: usage of a legacy numeric method
67+
//~| HELP: use the associated constant instead
68+
u128::MAX;
69+
//~^ ERROR: usage of a legacy numeric constant
70+
//~| HELP: use the associated constant instead
71+
u32::MAX;
72+
u128::MAX;
73+
f32::EPSILON;
74+
::std::primitive::u8::MIN;
75+
std::f32::consts::E;
76+
f64::consts::E;
77+
u8::MIN;
78+
std::f32::consts::E;
79+
f64::consts::E;
80+
b!();
81+
82+
[(0, "", i128::MAX)];
83+
//~^ ERROR: usage of a legacy numeric constant
84+
//~| HELP: use the associated constant instead
85+
}
86+
87+
#[warn(clippy::legacy_numeric_constants)]
88+
fn ext() {
89+
external! {
90+
::std::primitive::u8::MIN;
91+
::std::u8::MIN;
92+
::std::primitive::u8::min_value();
93+
use std::u64;
94+
use std::u8::MIN;
95+
}
96+
}
97+
98+
#[allow(clippy::legacy_numeric_constants)]
99+
fn allow() {
100+
::std::primitive::u8::MIN;
101+
::std::u8::MIN;
102+
::std::primitive::u8::min_value();
103+
use std::u64;
104+
use std::u8::MIN;
105+
}
106+
107+
#[warn(clippy::legacy_numeric_constants)]
108+
#[clippy::msrv = "1.42.0"]
109+
fn msrv_too_low() {
110+
std::u32::MAX;
111+
}
112+
113+
#[warn(clippy::legacy_numeric_constants)]
114+
#[clippy::msrv = "1.43.0"]
115+
fn msrv_juust_right() {
116+
u32::MAX;
117+
//~^ ERROR: usage of a legacy numeric constant
118+
}

‎tests/ui/legacy_numeric_constants.rs

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//@aux-build:proc_macros.rs
2+
#![allow(clippy::no_effect, deprecated, unused)]
3+
#![allow(clippy::legacy_numeric_constants)] // For imports.
4+
#![feature(lint_reasons)]
5+
6+
#[macro_use]
7+
extern crate proc_macros;
8+
9+
pub mod a {
10+
pub use std::u128;
11+
}
12+
13+
macro_rules! b {
14+
() => {
15+
mod b {
16+
#[warn(clippy::legacy_numeric_constants)]
17+
fn b() {
18+
let x = std::u64::MAX;
19+
//~^ ERROR: usage of a legacy numeric constant
20+
//~| HELP: use the associated constant instead
21+
}
22+
}
23+
};
24+
}
25+
26+
use std::u32::MAX;
27+
use std::u8::MIN;
28+
use std::{f64, u32};
29+
30+
#[warn(clippy::legacy_numeric_constants)]
31+
fn main() {
32+
std::f32::EPSILON;
33+
//~^ ERROR: usage of a legacy numeric constant
34+
//~| HELP: use the associated constant instead
35+
std::u8::MIN;
36+
//~^ ERROR: usage of a legacy numeric constant
37+
//~| HELP: use the associated constant instead
38+
std::usize::MIN;
39+
//~^ ERROR: usage of a legacy numeric constant
40+
//~| HELP: use the associated constant instead
41+
std::u32::MAX;
42+
//~^ ERROR: usage of a legacy numeric constant
43+
//~| HELP: use the associated constant instead
44+
core::u32::MAX;
45+
//~^ ERROR: usage of a legacy numeric constant
46+
//~| HELP: use the associated constant instead
47+
MAX;
48+
//~^ ERROR: usage of a legacy numeric constant
49+
//~| HELP: use the associated constant instead
50+
i32::max_value();
51+
//~^ ERROR: usage of a legacy numeric method
52+
//~| HELP: use the associated constant instead
53+
u8::max_value();
54+
//~^ ERROR: usage of a legacy numeric method
55+
//~| HELP: use the associated constant instead
56+
u8::min_value();
57+
//~^ ERROR: usage of a legacy numeric method
58+
//~| HELP: use the associated constant instead
59+
::std::u8::MIN;
60+
//~^ ERROR: usage of a legacy numeric constant
61+
//~| HELP: use the associated constant instead
62+
::std::primitive::u8::min_value();
63+
//~^ ERROR: usage of a legacy numeric method
64+
//~| HELP: use the associated constant instead
65+
std::primitive::i32::max_value();
66+
//~^ ERROR: usage of a legacy numeric method
67+
//~| HELP: use the associated constant instead
68+
self::a::u128::MAX;
69+
//~^ ERROR: usage of a legacy numeric constant
70+
//~| HELP: use the associated constant instead
71+
u32::MAX;
72+
u128::MAX;
73+
f32::EPSILON;
74+
::std::primitive::u8::MIN;
75+
std::f32::consts::E;
76+
f64::consts::E;
77+
u8::MIN;
78+
std::f32::consts::E;
79+
f64::consts::E;
80+
b!();
81+
82+
[(0, "", std::i128::MAX)];
83+
//~^ ERROR: usage of a legacy numeric constant
84+
//~| HELP: use the associated constant instead
85+
}
86+
87+
#[warn(clippy::legacy_numeric_constants)]
88+
fn ext() {
89+
external! {
90+
::std::primitive::u8::MIN;
91+
::std::u8::MIN;
92+
::std::primitive::u8::min_value();
93+
use std::u64;
94+
use std::u8::MIN;
95+
}
96+
}
97+
98+
#[allow(clippy::legacy_numeric_constants)]
99+
fn allow() {
100+
::std::primitive::u8::MIN;
101+
::std::u8::MIN;
102+
::std::primitive::u8::min_value();
103+
use std::u64;
104+
use std::u8::MIN;
105+
}
106+
107+
#[warn(clippy::legacy_numeric_constants)]
108+
#[clippy::msrv = "1.42.0"]
109+
fn msrv_too_low() {
110+
std::u32::MAX;
111+
}
112+
113+
#[warn(clippy::legacy_numeric_constants)]
114+
#[clippy::msrv = "1.43.0"]
115+
fn msrv_juust_right() {
116+
std::u32::MAX;
117+
//~^ ERROR: usage of a legacy numeric constant
118+
}
+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
error: usage of a legacy numeric constant
2+
--> tests/ui/legacy_numeric_constants.rs:32:5
3+
|
4+
LL | std::f32::EPSILON;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::legacy-numeric-constants` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::legacy_numeric_constants)]`
9+
help: use the associated constant instead
10+
|
11+
LL | f32::EPSILON;
12+
| ~~~~~~~~~~~~
13+
14+
error: usage of a legacy numeric constant
15+
--> tests/ui/legacy_numeric_constants.rs:35:5
16+
|
17+
LL | std::u8::MIN;
18+
| ^^^^^^^^^^^^
19+
|
20+
help: use the associated constant instead
21+
|
22+
LL | u8::MIN;
23+
| ~~~~~~~
24+
25+
error: usage of a legacy numeric constant
26+
--> tests/ui/legacy_numeric_constants.rs:38:5
27+
|
28+
LL | std::usize::MIN;
29+
| ^^^^^^^^^^^^^^^
30+
|
31+
help: use the associated constant instead
32+
|
33+
LL | usize::MIN;
34+
| ~~~~~~~~~~
35+
36+
error: usage of a legacy numeric constant
37+
--> tests/ui/legacy_numeric_constants.rs:41:5
38+
|
39+
LL | std::u32::MAX;
40+
| ^^^^^^^^^^^^^
41+
|
42+
help: use the associated constant instead
43+
|
44+
LL | u32::MAX;
45+
| ~~~~~~~~
46+
47+
error: usage of a legacy numeric constant
48+
--> tests/ui/legacy_numeric_constants.rs:44:5
49+
|
50+
LL | core::u32::MAX;
51+
| ^^^^^^^^^^^^^^
52+
|
53+
help: use the associated constant instead
54+
|
55+
LL | u32::MAX;
56+
| ~~~~~~~~
57+
58+
error: usage of a legacy numeric constant
59+
--> tests/ui/legacy_numeric_constants.rs:47:5
60+
|
61+
LL | MAX;
62+
| ^^^
63+
|
64+
help: use the associated constant instead
65+
|
66+
LL | u32::MAX;
67+
| ~~~~~~~~
68+
69+
error: usage of a legacy numeric method
70+
--> tests/ui/legacy_numeric_constants.rs:50:10
71+
|
72+
LL | i32::max_value();
73+
| ^^^^^^^^^^^
74+
|
75+
help: use the associated constant instead
76+
|
77+
LL | i32::MAX;
78+
| ~~~
79+
80+
error: usage of a legacy numeric method
81+
--> tests/ui/legacy_numeric_constants.rs:53:9
82+
|
83+
LL | u8::max_value();
84+
| ^^^^^^^^^^^
85+
|
86+
help: use the associated constant instead
87+
|
88+
LL | u8::MAX;
89+
| ~~~
90+
91+
error: usage of a legacy numeric method
92+
--> tests/ui/legacy_numeric_constants.rs:56:9
93+
|
94+
LL | u8::min_value();
95+
| ^^^^^^^^^^^
96+
|
97+
help: use the associated constant instead
98+
|
99+
LL | u8::MIN;
100+
| ~~~
101+
102+
error: usage of a legacy numeric constant
103+
--> tests/ui/legacy_numeric_constants.rs:59:5
104+
|
105+
LL | ::std::u8::MIN;
106+
| ^^^^^^^^^^^^^^
107+
|
108+
help: use the associated constant instead
109+
|
110+
LL | u8::MIN;
111+
| ~~~~~~~
112+
113+
error: usage of a legacy numeric method
114+
--> tests/ui/legacy_numeric_constants.rs:62:27
115+
|
116+
LL | ::std::primitive::u8::min_value();
117+
| ^^^^^^^^^^^
118+
|
119+
help: use the associated constant instead
120+
|
121+
LL | ::std::primitive::u8::MIN;
122+
| ~~~
123+
124+
error: usage of a legacy numeric method
125+
--> tests/ui/legacy_numeric_constants.rs:65:26
126+
|
127+
LL | std::primitive::i32::max_value();
128+
| ^^^^^^^^^^^
129+
|
130+
help: use the associated constant instead
131+
|
132+
LL | std::primitive::i32::MAX;
133+
| ~~~
134+
135+
error: usage of a legacy numeric constant
136+
--> tests/ui/legacy_numeric_constants.rs:68:5
137+
|
138+
LL | self::a::u128::MAX;
139+
| ^^^^^^^^^^^^^^^^^^
140+
|
141+
help: use the associated constant instead
142+
|
143+
LL | u128::MAX;
144+
| ~~~~~~~~~
145+
146+
error: usage of a legacy numeric constant
147+
--> tests/ui/legacy_numeric_constants.rs:18:25
148+
|
149+
LL | let x = std::u64::MAX;
150+
| ^^^^^^^^^^^^^
151+
...
152+
LL | b!();
153+
| ---- in this macro invocation
154+
|
155+
= note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info)
156+
help: use the associated constant instead
157+
|
158+
LL | let x = u64::MAX;
159+
| ~~~~~~~~
160+
161+
error: usage of a legacy numeric constant
162+
--> tests/ui/legacy_numeric_constants.rs:82:14
163+
|
164+
LL | [(0, "", std::i128::MAX)];
165+
| ^^^^^^^^^^^^^^
166+
|
167+
help: use the associated constant instead
168+
|
169+
LL | [(0, "", i128::MAX)];
170+
| ~~~~~~~~~
171+
172+
error: usage of a legacy numeric constant
173+
--> tests/ui/legacy_numeric_constants.rs:116:5
174+
|
175+
LL | std::u32::MAX;
176+
| ^^^^^^^^^^^^^
177+
|
178+
help: use the associated constant instead
179+
|
180+
LL | u32::MAX;
181+
| ~~~~~~~~
182+
183+
error: aborting due to 16 previous errors
184+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//@no-rustfix
2+
//@aux-build:proc_macros.rs
3+
#![allow(clippy::no_effect, deprecated, unused)]
4+
#![warn(clippy::legacy_numeric_constants)]
5+
#![feature(lint_reasons)]
6+
7+
#[macro_use]
8+
extern crate proc_macros;
9+
10+
use std::u128 as _;
11+
//~^ ERROR: importing legacy numeric constants
12+
//~| HELP: remove this import
13+
pub mod a {
14+
pub use std::{mem, u128};
15+
//~^ ERROR: importing legacy numeric constants
16+
//~| HELP: remove this import
17+
}
18+
19+
macro_rules! b {
20+
() => {
21+
mod b {
22+
use std::u32;
23+
//~^ ERROR: importing legacy numeric constants
24+
//~| HELP: remove this import
25+
}
26+
};
27+
}
28+
29+
fn main() {
30+
use std::u32::MAX;
31+
//~^ ERROR: importing a legacy numeric constant
32+
//~| HELP: remove this import and use the associated constant `u32::MAX`
33+
use std::u8::MIN;
34+
//~^ ERROR: importing a legacy numeric constant
35+
//~| HELP: remove this import and use the associated constant `u8::MIN`
36+
f64::MAX;
37+
use std::u32;
38+
//~^ ERROR: importing legacy numeric constants
39+
//~| HELP: remove this import
40+
u32::MAX;
41+
use std::f32::MIN_POSITIVE;
42+
//~^ ERROR: importing a legacy numeric constant
43+
//~| HELP: remove this import and use the associated constant `f32::MIN_POSITIVE`
44+
use std::f64;
45+
use std::i16::*;
46+
//~^ ERROR: importing legacy numeric constants
47+
//~| HELP: remove this import and use associated constants `i16::<CONST>`
48+
u128::MAX;
49+
f32::EPSILON;
50+
f64::EPSILON;
51+
::std::primitive::u8::MIN;
52+
std::f32::consts::E;
53+
f64::consts::E;
54+
u8::MIN;
55+
std::f32::consts::E;
56+
f64::consts::E;
57+
b!();
58+
}
59+
60+
fn ext() {
61+
external! {
62+
::std::primitive::u8::MIN;
63+
::std::u8::MIN;
64+
::std::primitive::u8::min_value();
65+
use std::u64;
66+
use std::u8::MIN;
67+
}
68+
}
69+
70+
#[clippy::msrv = "1.42.0"]
71+
fn msrv_too_low() {
72+
use std::u32::MAX;
73+
}
74+
75+
#[clippy::msrv = "1.43.0"]
76+
fn msrv_juust_right() {
77+
use std::u32::MAX;
78+
//~^ ERROR: importing a legacy numeric constant
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
error: importing legacy numeric constants
2+
--> tests/ui/legacy_numeric_constants_unfixable.rs:10:5
3+
|
4+
LL | use std::u128 as _;
5+
| ^^^^^^^^^
6+
|
7+
= help: remove this import
8+
= note: `-D clippy::legacy-numeric-constants` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::legacy_numeric_constants)]`
10+
11+
error: importing legacy numeric constants
12+
--> tests/ui/legacy_numeric_constants_unfixable.rs:14:24
13+
|
14+
LL | pub use std::{mem, u128};
15+
| ^^^^
16+
|
17+
= help: remove this import
18+
= note: then `u128::<CONST>` will resolve to the respective associated constant
19+
20+
error: importing a legacy numeric constant
21+
--> tests/ui/legacy_numeric_constants_unfixable.rs:30:9
22+
|
23+
LL | use std::u32::MAX;
24+
| ^^^^^^^^^^^^^
25+
|
26+
= help: remove this import and use the associated constant `u32::MAX` from the primitive type instead
27+
28+
error: importing a legacy numeric constant
29+
--> tests/ui/legacy_numeric_constants_unfixable.rs:33:9
30+
|
31+
LL | use std::u8::MIN;
32+
| ^^^^^^^^^^^^
33+
|
34+
= help: remove this import and use the associated constant `u8::MIN` from the primitive type instead
35+
36+
error: importing legacy numeric constants
37+
--> tests/ui/legacy_numeric_constants_unfixable.rs:37:9
38+
|
39+
LL | use std::u32;
40+
| ^^^^^^^^
41+
|
42+
= help: remove this import
43+
= note: then `u32::<CONST>` will resolve to the respective associated constant
44+
45+
error: importing a legacy numeric constant
46+
--> tests/ui/legacy_numeric_constants_unfixable.rs:41:9
47+
|
48+
LL | use std::f32::MIN_POSITIVE;
49+
| ^^^^^^^^^^^^^^^^^^^^^^
50+
|
51+
= help: remove this import and use the associated constant `f32::MIN_POSITIVE` from the primitive type instead
52+
53+
error: importing legacy numeric constants
54+
--> tests/ui/legacy_numeric_constants_unfixable.rs:45:9
55+
|
56+
LL | use std::i16::*;
57+
| ^^^^^^^^
58+
|
59+
= help: remove this import and use associated constants `i16::<CONST>` from the primitive type instead
60+
61+
error: importing legacy numeric constants
62+
--> tests/ui/legacy_numeric_constants_unfixable.rs:22:17
63+
|
64+
LL | use std::u32;
65+
| ^^^^^^^^
66+
...
67+
LL | b!();
68+
| ---- in this macro invocation
69+
|
70+
= help: remove this import
71+
= note: then `u32::<CONST>` will resolve to the respective associated constant
72+
= note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info)
73+
74+
error: importing a legacy numeric constant
75+
--> tests/ui/legacy_numeric_constants_unfixable.rs:77:9
76+
|
77+
LL | use std::u32::MAX;
78+
| ^^^^^^^^^^^^^
79+
|
80+
= help: remove this import and use the associated constant `u32::MAX` from the primitive type instead
81+
82+
error: aborting due to 9 previous errors
83+

‎tests/ui/manual_saturating_arithmetic.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused_imports)]
1+
#![allow(clippy::legacy_numeric_constants, unused_imports)]
22

33
use std::{i128, i32, u128, u32};
44

‎tests/ui/manual_saturating_arithmetic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused_imports)]
1+
#![allow(clippy::legacy_numeric_constants, unused_imports)]
22

33
use std::{i128, i32, u128, u32};
44

‎tests/ui/suspicious_arithmetic_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::legacy_numeric_constants)]
12
#![warn(clippy::suspicious_arithmetic_impl)]
23
use std::ops::{
34
Add, AddAssign, BitAnd, BitOr, BitOrAssign, BitXor, Div, DivAssign, Mul, MulAssign, Rem, Shl, Shr, Sub,

‎tests/ui/suspicious_arithmetic_impl.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: suspicious use of `-` in `Add` impl
2-
--> tests/ui/suspicious_arithmetic_impl.rs:13:20
2+
--> tests/ui/suspicious_arithmetic_impl.rs:14:20
33
|
44
LL | Foo(self.0 - other.0)
55
| ^
@@ -8,7 +8,7 @@ LL | Foo(self.0 - other.0)
88
= help: to override `-D warnings` add `#[allow(clippy::suspicious_arithmetic_impl)]`
99

1010
error: suspicious use of `-` in `AddAssign` impl
11-
--> tests/ui/suspicious_arithmetic_impl.rs:21:23
11+
--> tests/ui/suspicious_arithmetic_impl.rs:22:23
1212
|
1313
LL | *self = *self - other;
1414
| ^
@@ -17,43 +17,43 @@ LL | *self = *self - other;
1717
= help: to override `-D warnings` add `#[allow(clippy::suspicious_op_assign_impl)]`
1818

1919
error: suspicious use of `/` in `MulAssign` impl
20-
--> tests/ui/suspicious_arithmetic_impl.rs:36:16
20+
--> tests/ui/suspicious_arithmetic_impl.rs:37:16
2121
|
2222
LL | self.0 /= other.0;
2323
| ^^
2424

2525
error: suspicious use of `/` in `Rem` impl
26-
--> tests/ui/suspicious_arithmetic_impl.rs:75:20
26+
--> tests/ui/suspicious_arithmetic_impl.rs:76:20
2727
|
2828
LL | Foo(self.0 / other.0)
2929
| ^
3030

3131
error: suspicious use of `|` in `BitAnd` impl
32-
--> tests/ui/suspicious_arithmetic_impl.rs:84:20
32+
--> tests/ui/suspicious_arithmetic_impl.rs:85:20
3333
|
3434
LL | Foo(self.0 | other.0)
3535
| ^
3636

3737
error: suspicious use of `^` in `BitOr` impl
38-
--> tests/ui/suspicious_arithmetic_impl.rs:93:20
38+
--> tests/ui/suspicious_arithmetic_impl.rs:94:20
3939
|
4040
LL | Foo(self.0 ^ other.0)
4141
| ^
4242

4343
error: suspicious use of `&` in `BitXor` impl
44-
--> tests/ui/suspicious_arithmetic_impl.rs:102:20
44+
--> tests/ui/suspicious_arithmetic_impl.rs:103:20
4545
|
4646
LL | Foo(self.0 & other.0)
4747
| ^
4848

4949
error: suspicious use of `>>` in `Shl` impl
50-
--> tests/ui/suspicious_arithmetic_impl.rs:111:20
50+
--> tests/ui/suspicious_arithmetic_impl.rs:112:20
5151
|
5252
LL | Foo(self.0 >> other.0)
5353
| ^^
5454

5555
error: suspicious use of `<<` in `Shr` impl
56-
--> tests/ui/suspicious_arithmetic_impl.rs:120:20
56+
--> tests/ui/suspicious_arithmetic_impl.rs:121:20
5757
|
5858
LL | Foo(self.0 << other.0)
5959
| ^^

0 commit comments

Comments
 (0)
Please sign in to comment.