Skip to content

Commit 0b40983

Browse files
committed
Auto merge of rust-lang#5429 - faern:use-assoc-int-float-consts, r=flip1995
Use assoc int and float consts instead of module level ones changelog: Recommend primitive type associated constants instead of module level constants In Rust 1.43 integer and float primitive types will have a number of new associated constants. For example `MAX`, `MIN` and a number of constants related to the machine representation of floats. rust-lang#68952 These new constants are preferred over the module level constants in `{core,std}::{f*, u*, i*}`. I have in the last few days made sure that the documentation in the main rust repository uses the new constants in every place I could find (rust-lang#69860, rust-lang#70782). So the next step is naturally to make the linter recommend the new constants as well. This PR only changes two lints. There are more. But I did not want the PR to be too big. And since I have not contributed to clippy before it felt saner to start with a small PR so I see if there are any quirks. More will come later.
2 parents d342cee + 1647f53 commit 0b40983

24 files changed

+138
-142
lines changed

clippy_lints/src/checked_conversions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ declare_clippy_lint! {
2121
/// ```rust
2222
/// # let foo: u32 = 5;
2323
/// # let _ =
24-
/// foo <= i32::max_value() as u32
24+
/// foo <= i32::MAX as u32
2525
/// # ;
2626
/// ```
2727
///
@@ -179,7 +179,7 @@ impl ConversionType {
179179
}
180180
}
181181

182-
/// Check for `expr <= (to_type::max_value() as from_type)`
182+
/// Check for `expr <= (to_type::MAX as from_type)`
183183
fn check_upper_bound<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
184184
if_chain! {
185185
if let ExprKind::Binary(ref op, ref left, ref right) = &expr.kind;
@@ -194,7 +194,7 @@ fn check_upper_bound<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
194194
}
195195
}
196196

197-
/// Check for `expr >= 0|(to_type::min_value() as from_type)`
197+
/// Check for `expr >= 0|(to_type::MIN as from_type)`
198198
fn check_lower_bound<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
199199
fn check_function<'a>(candidate: &'a Expr<'a>, check: &'a Expr<'a>) -> Option<Conversion<'a>> {
200200
(check_lower_bound_zero(candidate, check)).or_else(|| (check_lower_bound_min(candidate, check)))
@@ -222,7 +222,7 @@ fn check_lower_bound_zero<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> O
222222
}
223223
}
224224

225-
/// Check for `expr >= (to_type::min_value() as from_type)`
225+
/// Check for `expr >= (to_type::MIN as from_type)`
226226
fn check_lower_bound_min<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> Option<Conversion<'a>> {
227227
if let Some((from, to)) = get_types_from_cast(check, MIN_VALUE, SINTS) {
228228
Conversion::try_new(candidate, from, to)

clippy_lints/src/float_literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hir as hir;
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::ty;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
9-
use std::{f32, f64, fmt};
9+
use std::fmt;
1010

1111
declare_clippy_lint! {
1212
/// **What it does:** Checks for float literals with a precision greater

clippy_lints/src/methods/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,8 @@ declare_clippy_lint! {
11381138
/// ```rust
11391139
/// # let y: u32 = 0;
11401140
/// # let x: u32 = 100;
1141-
/// let add = x.checked_add(y).unwrap_or(u32::max_value());
1142-
/// let sub = x.checked_sub(y).unwrap_or(u32::min_value());
1141+
/// let add = x.checked_add(y).unwrap_or(u32::MAX);
1142+
/// let sub = x.checked_sub(y).unwrap_or(u32::MIN);
11431143
/// ```
11441144
///
11451145
/// can be written using dedicated methods for saturating addition/subtraction as:

clippy_lints/src/misc.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ declare_clippy_lint! {
5757
///
5858
/// **Example:**
5959
/// ```rust
60-
/// # use core::f32::NAN;
6160
/// # let x = 1.0;
6261
///
63-
/// if x == NAN { }
62+
/// if x == f32::NAN { }
6463
/// ```
6564
pub CMP_NAN,
6665
correctness,
@@ -389,7 +388,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
389388
),
390389
Applicability::HasPlaceholders, // snippet
391390
);
392-
db.span_note(expr.span, "`std::f32::EPSILON` and `std::f64::EPSILON` are available.");
391+
db.span_note(expr.span, "`f32::EPSILON` and `f64::EPSILON` are available.");
393392
});
394393
} else if op == BinOpKind::Rem && is_integer_const(cx, right, 1) {
395394
span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");
@@ -457,7 +456,7 @@ fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr<'_>, cmp_expr: &Expr<'_>) {
457456
cx,
458457
CMP_NAN,
459458
cmp_expr.span,
460-
"doomed comparison with `NAN`, use `std::{f32,f64}::is_nan()` instead",
459+
"doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead",
461460
);
462461
}
463462
}

clippy_lints/src/neg_cmp_op_on_partial_ord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ declare_clippy_lint! {
2525
///
2626
/// // Bad
2727
/// let a = 1.0;
28-
/// let b = std::f64::NAN;
28+
/// let b = f64::NAN;
2929
///
3030
/// let _not_less_or_equal = !(a <= b);
3131
///
3232
/// // Good
3333
/// let a = 1.0;
34-
/// let b = std::f64::NAN;
34+
/// let b = f64::NAN;
3535
///
3636
/// let _not_less_or_equal = match a.partial_cmp(&b) {
3737
/// None | Some(Ordering::Greater) => true,

clippy_lints/src/types.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ declare_clippy_lint! {
837837
///
838838
/// **Example:**
839839
/// ```rust
840-
/// let x = std::u64::MAX;
840+
/// let x = u64::MAX;
841841
/// x as f64;
842842
/// ```
843843
pub CAST_PRECISION_LOSS,
@@ -904,7 +904,7 @@ declare_clippy_lint! {
904904
///
905905
/// **Example:**
906906
/// ```rust
907-
/// std::u32::MAX as i32; // will yield a value of `-1`
907+
/// u32::MAX as i32; // will yield a value of `-1`
908908
/// ```
909909
pub CAST_POSSIBLE_WRAP,
910910
pedantic,
@@ -1752,7 +1752,7 @@ declare_clippy_lint! {
17521752
/// ```rust
17531753
/// let vec: Vec<isize> = Vec::new();
17541754
/// if vec.len() <= 0 {}
1755-
/// if 100 > std::i32::MAX {}
1755+
/// if 100 > i32::MAX {}
17561756
/// ```
17571757
pub ABSURD_EXTREME_COMPARISONS,
17581758
correctness,
@@ -1973,8 +1973,6 @@ impl Ord for FullInt {
19731973
}
19741974

19751975
fn numeric_cast_precast_bounds<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'_>) -> Option<(FullInt, FullInt)> {
1976-
use std::{i128, i16, i32, i64, i8, isize, u128, u16, u32, u64, u8, usize};
1977-
19781976
if let ExprKind::Cast(ref cast_exp, _) = expr.kind {
19791977
let pre_cast_ty = cx.tables.expr_ty(cast_exp);
19801978
let cast_ty = cx.tables.expr_ty(expr);

clippy_lints/src/utils/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<Mult
6060
/// 6 | let other_f64_nan = 0.0f64 / 0.0;
6161
/// | ^^^^^^^^^^^^
6262
/// |
63-
/// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN
63+
/// = help: Consider using `f64::NAN` if you would like a constant representing NaN
6464
/// ```
6565
pub fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {
6666
cx.struct_span_lint(lint, span, |ldb| {

clippy_lints/src/zero_div_zero.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
88
declare_clippy_lint! {
99
/// **What it does:** Checks for `0.0 / 0.0`.
1010
///
11-
/// **Why is this bad?** It's less readable than `std::f32::NAN` or
12-
/// `std::f64::NAN`.
11+
/// **Why is this bad?** It's less readable than `f32::NAN` or `f64::NAN`.
1312
///
1413
/// **Known problems:** None.
1514
///
@@ -19,7 +18,7 @@ declare_clippy_lint! {
1918
/// ```
2019
pub ZERO_DIVIDED_BY_ZERO,
2120
complexity,
22-
"usage of `0.0 / 0.0` to obtain NaN instead of `std::f32::NAN` or `std::f64::NAN`"
21+
"usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN`"
2322
}
2423

2524
declare_lint_pass!(ZeroDiv => [ZERO_DIVIDED_BY_ZERO]);
@@ -38,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv {
3837
if Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value;
3938
if Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value;
4039
then {
41-
// since we're about to suggest a use of std::f32::NaN or std::f64::NaN,
40+
// since we're about to suggest a use of f32::NAN or f64::NAN,
4241
// match the precision of the literals that are given.
4342
let float_type = match (lhs_value, rhs_value) {
4443
(Constant::F64(_), _)
@@ -51,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv {
5150
expr.span,
5251
"constant division of `0.0` with `0.0` will always result in NaN",
5352
&format!(
54-
"Consider using `std::{}::NAN` if you would like a constant representing NaN",
53+
"Consider using `{}::NAN` if you would like a constant representing NaN",
5554
float_type,
5655
),
5756
);

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
25262526
Lint {
25272527
name: "zero_divided_by_zero",
25282528
group: "complexity",
2529-
desc: "usage of `0.0 / 0.0` to obtain NaN instead of `std::f32::NAN` or `std::f64::NAN`",
2529+
desc: "usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN`",
25302530
deprecation: None,
25312531
module: "zero_div_zero",
25322532
},

tests/ui/absurd-extreme-comparisons.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ fn main() {
1616
u < Z;
1717
Z >= u;
1818
Z > u;
19-
u > std::u32::MAX;
20-
u >= std::u32::MAX;
21-
std::u32::MAX < u;
22-
std::u32::MAX <= u;
19+
u > u32::MAX;
20+
u >= u32::MAX;
21+
u32::MAX < u;
22+
u32::MAX <= u;
2323
1-1 > u;
2424
u >= !0;
2525
u <= 12 - 2*6;
2626
let i: i8 = 0;
2727
i < -127 - 1;
28-
std::i8::MAX >= i;
29-
3-7 < std::i32::MIN;
28+
i8::MAX >= i;
29+
3-7 < i32::MIN;
3030
let b = false;
3131
b >= true;
3232
false > b;
@@ -52,10 +52,10 @@ impl PartialOrd<u32> for U {
5252
}
5353

5454
pub fn foo(val: U) -> bool {
55-
val > std::u32::MAX
55+
val > u32::MAX
5656
}
5757

5858
pub fn bar(len: u64) -> bool {
5959
// This is OK as we are casting from target sized to fixed size
60-
len >= std::usize::MAX as u64
60+
len >= usize::MAX as u64
6161
}

tests/ui/absurd-extreme-comparisons.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,34 @@ LL | Z > u;
4242
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
4343
--> $DIR/absurd-extreme-comparisons.rs:19:5
4444
|
45-
LL | u > std::u32::MAX;
46-
| ^^^^^^^^^^^^^^^^^
45+
LL | u > u32::MAX;
46+
| ^^^^^^^^^^^^
4747
|
48-
= help: because `std::u32::MAX` is the maximum value for this type, this comparison is always false
48+
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false
4949

5050
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
5151
--> $DIR/absurd-extreme-comparisons.rs:20:5
5252
|
53-
LL | u >= std::u32::MAX;
54-
| ^^^^^^^^^^^^^^^^^^
53+
LL | u >= u32::MAX;
54+
| ^^^^^^^^^^^^^
5555
|
56-
= help: because `std::u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u == std::u32::MAX` instead
56+
= help: because `u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u == u32::MAX` instead
5757

5858
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
5959
--> $DIR/absurd-extreme-comparisons.rs:21:5
6060
|
61-
LL | std::u32::MAX < u;
62-
| ^^^^^^^^^^^^^^^^^
61+
LL | u32::MAX < u;
62+
| ^^^^^^^^^^^^
6363
|
64-
= help: because `std::u32::MAX` is the maximum value for this type, this comparison is always false
64+
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false
6565

6666
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
6767
--> $DIR/absurd-extreme-comparisons.rs:22:5
6868
|
69-
LL | std::u32::MAX <= u;
70-
| ^^^^^^^^^^^^^^^^^^
69+
LL | u32::MAX <= u;
70+
| ^^^^^^^^^^^^^
7171
|
72-
= help: because `std::u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `std::u32::MAX == u` instead
72+
= help: because `u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u32::MAX == u` instead
7373

7474
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
7575
--> $DIR/absurd-extreme-comparisons.rs:23:5
@@ -106,18 +106,18 @@ LL | i < -127 - 1;
106106
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
107107
--> $DIR/absurd-extreme-comparisons.rs:28:5
108108
|
109-
LL | std::i8::MAX >= i;
110-
| ^^^^^^^^^^^^^^^^^
109+
LL | i8::MAX >= i;
110+
| ^^^^^^^^^^^^
111111
|
112-
= help: because `std::i8::MAX` is the maximum value for this type, this comparison is always true
112+
= help: because `i8::MAX` is the maximum value for this type, this comparison is always true
113113

114114
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
115115
--> $DIR/absurd-extreme-comparisons.rs:29:5
116116
|
117-
LL | 3-7 < std::i32::MIN;
118-
| ^^^^^^^^^^^^^^^^^^^
117+
LL | 3-7 < i32::MIN;
118+
| ^^^^^^^^^^^^^^
119119
|
120-
= help: because `std::i32::MIN` is the minimum value for this type, this comparison is always false
120+
= help: because `i32::MIN` is the minimum value for this type, this comparison is always false
121121

122122
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
123123
--> $DIR/absurd-extreme-comparisons.rs:31:5

tests/ui/cmp_nan.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const NAN_F32: f32 = std::f32::NAN;
2-
const NAN_F64: f64 = std::f64::NAN;
1+
const NAN_F32: f32 = f32::NAN;
2+
const NAN_F64: f64 = f64::NAN;
33

44
#[warn(clippy::cmp_nan)]
55
#[allow(clippy::float_cmp, clippy::no_effect, clippy::unnecessary_operation)]
66
fn main() {
77
let x = 5f32;
8-
x == std::f32::NAN;
9-
x != std::f32::NAN;
10-
x < std::f32::NAN;
11-
x > std::f32::NAN;
12-
x <= std::f32::NAN;
13-
x >= std::f32::NAN;
8+
x == f32::NAN;
9+
x != f32::NAN;
10+
x < f32::NAN;
11+
x > f32::NAN;
12+
x <= f32::NAN;
13+
x >= f32::NAN;
1414
x == NAN_F32;
1515
x != NAN_F32;
1616
x < NAN_F32;
@@ -19,12 +19,12 @@ fn main() {
1919
x >= NAN_F32;
2020

2121
let y = 0f64;
22-
y == std::f64::NAN;
23-
y != std::f64::NAN;
24-
y < std::f64::NAN;
25-
y > std::f64::NAN;
26-
y <= std::f64::NAN;
27-
y >= std::f64::NAN;
22+
y == f64::NAN;
23+
y != f64::NAN;
24+
y < f64::NAN;
25+
y > f64::NAN;
26+
y <= f64::NAN;
27+
y >= f64::NAN;
2828
y == NAN_F64;
2929
y != NAN_F64;
3030
y < NAN_F64;

0 commit comments

Comments
 (0)