Skip to content

Commit

Permalink
Auto merge of #5429 - faern:use-assoc-int-float-consts, r=flip1995
Browse files Browse the repository at this point in the history
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/rust#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/rust#69860, rust-lang/rust#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.
  • Loading branch information
bors committed Apr 8, 2020
2 parents d342cee + 1647f53 commit 0b40983
Show file tree
Hide file tree
Showing 24 changed files with 138 additions and 142 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/checked_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare_clippy_lint! {
/// ```rust
/// # let foo: u32 = 5;
/// # let _ =
/// foo <= i32::max_value() as u32
/// foo <= i32::MAX as u32
/// # ;
/// ```
///
Expand Down Expand Up @@ -179,7 +179,7 @@ impl ConversionType {
}
}

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

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

/// Check for `expr >= (to_type::min_value() as from_type)`
/// Check for `expr >= (to_type::MIN as from_type)`
fn check_lower_bound_min<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> Option<Conversion<'a>> {
if let Some((from, to)) = get_types_from_cast(check, MIN_VALUE, SINTS) {
Conversion::try_new(candidate, from, to)
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/float_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::{f32, f64, fmt};
use std::fmt;

declare_clippy_lint! {
/// **What it does:** Checks for float literals with a precision greater
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,8 +1138,8 @@ declare_clippy_lint! {
/// ```rust
/// # let y: u32 = 0;
/// # let x: u32 = 100;
/// let add = x.checked_add(y).unwrap_or(u32::max_value());
/// let sub = x.checked_sub(y).unwrap_or(u32::min_value());
/// let add = x.checked_add(y).unwrap_or(u32::MAX);
/// let sub = x.checked_sub(y).unwrap_or(u32::MIN);
/// ```
///
/// can be written using dedicated methods for saturating addition/subtraction as:
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # use core::f32::NAN;
/// # let x = 1.0;
///
/// if x == NAN { }
/// if x == f32::NAN { }
/// ```
pub CMP_NAN,
correctness,
Expand Down Expand Up @@ -389,7 +388,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
),
Applicability::HasPlaceholders, // snippet
);
db.span_note(expr.span, "`std::f32::EPSILON` and `std::f64::EPSILON` are available.");
db.span_note(expr.span, "`f32::EPSILON` and `f64::EPSILON` are available.");
});
} else if op == BinOpKind::Rem && is_integer_const(cx, right, 1) {
span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");
Expand Down Expand Up @@ -457,7 +456,7 @@ fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr<'_>, cmp_expr: &Expr<'_>) {
cx,
CMP_NAN,
cmp_expr.span,
"doomed comparison with `NAN`, use `std::{f32,f64}::is_nan()` instead",
"doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead",
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ declare_clippy_lint! {
///
/// // Bad
/// let a = 1.0;
/// let b = std::f64::NAN;
/// let b = f64::NAN;
///
/// let _not_less_or_equal = !(a <= b);
///
/// // Good
/// let a = 1.0;
/// let b = std::f64::NAN;
/// let b = f64::NAN;
///
/// let _not_less_or_equal = match a.partial_cmp(&b) {
/// None | Some(Ordering::Greater) => true,
Expand Down
8 changes: 3 additions & 5 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let x = std::u64::MAX;
/// let x = u64::MAX;
/// x as f64;
/// ```
pub CAST_PRECISION_LOSS,
Expand Down Expand Up @@ -904,7 +904,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// std::u32::MAX as i32; // will yield a value of `-1`
/// u32::MAX as i32; // will yield a value of `-1`
/// ```
pub CAST_POSSIBLE_WRAP,
pedantic,
Expand Down Expand Up @@ -1752,7 +1752,7 @@ declare_clippy_lint! {
/// ```rust
/// let vec: Vec<isize> = Vec::new();
/// if vec.len() <= 0 {}
/// if 100 > std::i32::MAX {}
/// if 100 > i32::MAX {}
/// ```
pub ABSURD_EXTREME_COMPARISONS,
correctness,
Expand Down Expand Up @@ -1973,8 +1973,6 @@ impl Ord for FullInt {
}

fn numeric_cast_precast_bounds<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'_>) -> Option<(FullInt, FullInt)> {
use std::{i128, i16, i32, i64, i8, isize, u128, u16, u32, u64, u8, usize};

if let ExprKind::Cast(ref cast_exp, _) = expr.kind {
let pre_cast_ty = cx.tables.expr_ty(cast_exp);
let cast_ty = cx.tables.expr_ty(expr);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<Mult
/// 6 | let other_f64_nan = 0.0f64 / 0.0;
/// | ^^^^^^^^^^^^
/// |
/// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN
/// = help: Consider using `f64::NAN` if you would like a constant representing NaN
/// ```
pub fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {
cx.struct_span_lint(lint, span, |ldb| {
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/zero_div_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// **What it does:** Checks for `0.0 / 0.0`.
///
/// **Why is this bad?** It's less readable than `std::f32::NAN` or
/// `std::f64::NAN`.
/// **Why is this bad?** It's less readable than `f32::NAN` or `f64::NAN`.
///
/// **Known problems:** None.
///
Expand All @@ -19,7 +18,7 @@ declare_clippy_lint! {
/// ```
pub ZERO_DIVIDED_BY_ZERO,
complexity,
"usage of `0.0 / 0.0` to obtain NaN instead of `std::f32::NAN` or `std::f64::NAN`"
"usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN`"
}

declare_lint_pass!(ZeroDiv => [ZERO_DIVIDED_BY_ZERO]);
Expand All @@ -38,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv {
if Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value;
if Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value;
then {
// since we're about to suggest a use of std::f32::NaN or std::f64::NaN,
// since we're about to suggest a use of f32::NAN or f64::NAN,
// match the precision of the literals that are given.
let float_type = match (lhs_value, rhs_value) {
(Constant::F64(_), _)
Expand All @@ -51,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv {
expr.span,
"constant division of `0.0` with `0.0` will always result in NaN",
&format!(
"Consider using `std::{}::NAN` if you would like a constant representing NaN",
"Consider using `{}::NAN` if you would like a constant representing NaN",
float_type,
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2526,7 +2526,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
Lint {
name: "zero_divided_by_zero",
group: "complexity",
desc: "usage of `0.0 / 0.0` to obtain NaN instead of `std::f32::NAN` or `std::f64::NAN`",
desc: "usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN`",
deprecation: None,
module: "zero_div_zero",
},
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/absurd-extreme-comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ fn main() {
u < Z;
Z >= u;
Z > u;
u > std::u32::MAX;
u >= std::u32::MAX;
std::u32::MAX < u;
std::u32::MAX <= u;
u > u32::MAX;
u >= u32::MAX;
u32::MAX < u;
u32::MAX <= u;
1-1 > u;
u >= !0;
u <= 12 - 2*6;
let i: i8 = 0;
i < -127 - 1;
std::i8::MAX >= i;
3-7 < std::i32::MIN;
i8::MAX >= i;
3-7 < i32::MIN;
let b = false;
b >= true;
false > b;
Expand All @@ -52,10 +52,10 @@ impl PartialOrd<u32> for U {
}

pub fn foo(val: U) -> bool {
val > std::u32::MAX
val > u32::MAX
}

pub fn bar(len: u64) -> bool {
// This is OK as we are casting from target sized to fixed size
len >= std::usize::MAX as u64
len >= usize::MAX as u64
}
36 changes: 18 additions & 18 deletions tests/ui/absurd-extreme-comparisons.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,34 @@ LL | Z > u;
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:19:5
|
LL | u > std::u32::MAX;
| ^^^^^^^^^^^^^^^^^
LL | u > u32::MAX;
| ^^^^^^^^^^^^
|
= help: because `std::u32::MAX` is the maximum value for this type, this comparison is always false
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:20:5
|
LL | u >= std::u32::MAX;
| ^^^^^^^^^^^^^^^^^^
LL | u >= u32::MAX;
| ^^^^^^^^^^^^^
|
= 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
= 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

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

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:22:5
|
LL | std::u32::MAX <= u;
| ^^^^^^^^^^^^^^^^^^
LL | u32::MAX <= u;
| ^^^^^^^^^^^^^
|
= 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
= 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

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

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

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:31:5
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/cmp_nan.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const NAN_F32: f32 = std::f32::NAN;
const NAN_F64: f64 = std::f64::NAN;
const NAN_F32: f32 = f32::NAN;
const NAN_F64: f64 = f64::NAN;

#[warn(clippy::cmp_nan)]
#[allow(clippy::float_cmp, clippy::no_effect, clippy::unnecessary_operation)]
fn main() {
let x = 5f32;
x == std::f32::NAN;
x != std::f32::NAN;
x < std::f32::NAN;
x > std::f32::NAN;
x <= std::f32::NAN;
x >= std::f32::NAN;
x == f32::NAN;
x != f32::NAN;
x < f32::NAN;
x > f32::NAN;
x <= f32::NAN;
x >= f32::NAN;
x == NAN_F32;
x != NAN_F32;
x < NAN_F32;
Expand All @@ -19,12 +19,12 @@ fn main() {
x >= NAN_F32;

let y = 0f64;
y == std::f64::NAN;
y != std::f64::NAN;
y < std::f64::NAN;
y > std::f64::NAN;
y <= std::f64::NAN;
y >= std::f64::NAN;
y == f64::NAN;
y != f64::NAN;
y < f64::NAN;
y > f64::NAN;
y <= f64::NAN;
y >= f64::NAN;
y == NAN_F64;
y != NAN_F64;
y < NAN_F64;
Expand Down
Loading

0 comments on commit 0b40983

Please sign in to comment.