@@ -2,14 +2,15 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
22use clippy_utils:: diagnostics:: span_lint_and_help;
33use rustc_hir:: { BinOpKind , Expr , ExprKind } ;
44use rustc_lint:: { LateContext , LateLintPass } ;
5+ use rustc_middle:: ty;
56use rustc_session:: declare_lint_pass;
67
78declare_clippy_lint ! {
89 /// ### What it does
910 /// Checks for `0.0 / 0.0`.
1011 ///
1112 /// ### Why is this bad?
12- /// It's less readable than `f32::NAN` or `f64:: NAN`.
13+ /// It's less readable than using the proper associated ` NAN` constant .
1314 ///
1415 /// ### Example
1516 /// ```no_run
@@ -23,7 +24,7 @@ declare_clippy_lint! {
2324 #[ clippy:: version = "pre 1.29.0" ]
2425 pub ZERO_DIVIDED_BY_ZERO ,
2526 complexity,
26- "usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN` "
27+ "usage of `0.0 / 0.0` to obtain NaN instead of using a predefined constant "
2728}
2829
2930declare_lint_pass ! ( ZeroDiv => [ ZERO_DIVIDED_BY_ZERO ] ) ;
@@ -40,23 +41,20 @@ impl<'tcx> LateLintPass<'tcx> for ZeroDiv {
4041 && let ctxt = expr. span . ctxt ( )
4142 && let Some ( lhs_value) = ecx. eval_local ( left, ctxt)
4243 && let Some ( rhs_value) = ecx. eval_local ( right, ctxt)
43- // FIXME(f16_f128): add these types when eq is available on all platforms
44- && ( Constant :: F32 ( 0.0 ) == lhs_value || Constant :: F64 ( 0.0 ) == lhs_value )
45- && ( Constant :: F32 ( 0.0 ) == rhs_value || Constant :: F64 ( 0.0 ) == rhs_value )
44+ && matches ! ( lhs_value , Constant :: F16 ( 0.0 ) | Constant :: F32 ( 0.0 ) | Constant :: F64 ( 0.0 ) | Constant :: F128 ( 0.0 ) )
45+ && matches ! ( rhs_value , Constant :: F16 ( 0.0 ) | Constant :: F32 ( 0.0 ) | Constant :: F64 ( 0.0 ) | Constant :: F128 ( 0.0 ) )
46+ && let ty :: Float ( float_ty ) = cx . typeck_results ( ) . expr_ty ( expr ) . kind ( )
4647 {
47- // since we're about to suggest a use of f32::NAN or f64::NAN,
48- // match the precision of the literals that are given.
49- let float_type = match ( lhs_value, rhs_value) {
50- ( Constant :: F64 ( _) , _) | ( _, Constant :: F64 ( _) ) => "f64" ,
51- _ => "f32" ,
52- } ;
5348 span_lint_and_help (
5449 cx,
5550 ZERO_DIVIDED_BY_ZERO ,
5651 expr. span ,
5752 "constant division of `0.0` with `0.0` will always result in NaN" ,
5853 None ,
59- format ! ( "consider using `{float_type}::NAN` if you would like a constant representing NaN" , ) ,
54+ format ! (
55+ "consider using `{}::NAN` if you would like a constant representing NaN" ,
56+ float_ty. name_str( )
57+ ) ,
6058 ) ;
6159 }
6260 }
0 commit comments