@@ -617,6 +617,55 @@ fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
617
617
}
618
618
}
619
619
620
+ fn check_radians ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) {
621
+ if_chain ! {
622
+ if let ExprKind :: Binary (
623
+ Spanned {
624
+ node: BinOpKind :: Div , ..
625
+ } ,
626
+ div_lhs,
627
+ div_rhs,
628
+ ) = & expr. kind;
629
+ if let ExprKind :: Binary (
630
+ Spanned {
631
+ node: BinOpKind :: Mul , ..
632
+ } ,
633
+ mul_lhs,
634
+ mul_rhs,
635
+ ) = & div_lhs. kind;
636
+ if let Some ( ( rvalue, _) ) = constant( cx, cx. tables, div_rhs) ;
637
+ if let Some ( ( lvalue, _) ) = constant( cx, cx. tables, mul_rhs) ;
638
+ then {
639
+ if ( F32 ( f32_consts:: PI ) == rvalue || F64 ( f64_consts:: PI ) == rvalue) &&
640
+ ( F32 ( 180_f32 ) == lvalue || F64 ( 180_f64 ) == lvalue)
641
+ {
642
+ span_lint_and_sugg(
643
+ cx,
644
+ IMPRECISE_FLOPS ,
645
+ expr. span,
646
+ "conversion to degrees can be done more accurately" ,
647
+ "consider using" ,
648
+ format!( "{}.to_degrees()" , Sugg :: hir( cx, & mul_lhs, ".." ) ) ,
649
+ Applicability :: MachineApplicable ,
650
+ ) ;
651
+ } else if
652
+ ( F32 ( 180_f32 ) == rvalue || F64 ( 180_f64 ) == rvalue) &&
653
+ ( F32 ( f32_consts:: PI ) == lvalue || F64 ( f64_consts:: PI ) == lvalue)
654
+ {
655
+ span_lint_and_sugg(
656
+ cx,
657
+ IMPRECISE_FLOPS ,
658
+ expr. span,
659
+ "conversion to radians can be done more accurately" ,
660
+ "consider using" ,
661
+ format!( "{}.to_radians()" , Sugg :: hir( cx, & mul_lhs, ".." ) ) ,
662
+ Applicability :: MachineApplicable ,
663
+ ) ;
664
+ }
665
+ }
666
+ }
667
+ }
668
+
620
669
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for FloatingPointArithmetic {
621
670
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
622
671
if let ExprKind :: MethodCall ( ref path, _, args) = & expr. kind {
@@ -637,6 +686,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
637
686
check_mul_add ( cx, expr) ;
638
687
check_custom_abs ( cx, expr) ;
639
688
check_log_division ( cx, expr) ;
689
+ check_radians ( cx, expr) ;
640
690
}
641
691
}
642
692
}
0 commit comments