@@ -2,10 +2,10 @@ use crate::consts::{
22 constant, constant_simple, Constant ,
33 Constant :: { Int , F32 , F64 } ,
44} ;
5- use crate :: utils:: { higher, numeric_literal, span_lint_and_sugg, sugg, SpanlessEq } ;
5+ use crate :: utils:: { get_parent_expr , higher, numeric_literal, span_lint_and_sugg, sugg, SpanlessEq } ;
66use if_chain:: if_chain;
77use rustc_errors:: Applicability ;
8- use rustc_hir:: { BinOpKind , Expr , ExprKind , UnOp } ;
8+ use rustc_hir:: { BinOpKind , Expr , ExprKind , PathSegment , UnOp } ;
99use rustc_lint:: { LateContext , LateLintPass } ;
1010use rustc_middle:: ty;
1111use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -298,6 +298,17 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
298298fn check_powi ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , args : & [ Expr < ' _ > ] ) {
299299 // Check argument
300300 if let Some ( ( value, _) ) = constant ( cx, cx. tables , & args[ 1 ] ) {
301+ // TODO: need more specific check. this is too wide. remember also to include tests
302+ if let Some ( parent) = get_parent_expr ( cx, expr) {
303+ if let Some ( grandparent) = get_parent_expr ( cx, parent) {
304+ if let ExprKind :: MethodCall ( PathSegment { ident : method_name, .. } , _, args) = grandparent. kind {
305+ if method_name. as_str ( ) == "sqrt" && detect_hypot ( cx, args) . is_some ( ) {
306+ return ;
307+ }
308+ }
309+ }
310+ }
311+
301312 let ( lint, help, suggestion) = match value {
302313 Int ( 2 ) => (
303314 IMPRECISE_FLOPS ,
@@ -319,6 +330,57 @@ fn check_powi(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
319330 }
320331}
321332
333+ fn detect_hypot ( cx : & LateContext < ' _ , ' _ > , args : & [ Expr < ' _ > ] ) -> Option < String > {
334+ if let ExprKind :: Binary (
335+ Spanned {
336+ node : BinOpKind :: Add , ..
337+ } ,
338+ ref add_lhs,
339+ ref add_rhs,
340+ ) = args[ 0 ] . kind
341+ {
342+ // check if expression of the form x * x + y * y
343+ if_chain ! {
344+ if let ExprKind :: Binary ( Spanned { node: BinOpKind :: Mul , .. } , ref lmul_lhs, ref lmul_rhs) = add_lhs. kind;
345+ if let ExprKind :: Binary ( Spanned { node: BinOpKind :: Mul , .. } , ref rmul_lhs, ref rmul_rhs) = add_rhs. kind;
346+ if are_exprs_equal( cx, lmul_lhs, lmul_rhs) ;
347+ if are_exprs_equal( cx, rmul_lhs, rmul_rhs) ;
348+ then {
349+ return Some ( format!( "{}.hypot({})" , Sugg :: hir( cx, & lmul_lhs, ".." ) , Sugg :: hir( cx, & rmul_lhs, ".." ) ) ) ;
350+ }
351+ }
352+
353+ // check if expression of the form x.powi(2) + y.powi(2)
354+ if_chain ! {
355+ if let ExprKind :: MethodCall ( PathSegment { ident: lmethod_name, .. } , ref _lspan, ref largs) = add_lhs. kind;
356+ if let ExprKind :: MethodCall ( PathSegment { ident: rmethod_name, .. } , ref _rspan, ref rargs) = add_rhs. kind;
357+ if lmethod_name. as_str( ) == "powi" && rmethod_name. as_str( ) == "powi" ;
358+ if let Some ( ( lvalue, _) ) = constant( cx, cx. tables, & largs[ 1 ] ) ;
359+ if let Some ( ( rvalue, _) ) = constant( cx, cx. tables, & rargs[ 1 ] ) ;
360+ if Int ( 2 ) == lvalue && Int ( 2 ) == rvalue;
361+ then {
362+ return Some ( format!( "{}.hypot({})" , Sugg :: hir( cx, & largs[ 0 ] , ".." ) , Sugg :: hir( cx, & rargs[ 0 ] , ".." ) ) ) ;
363+ }
364+ }
365+ }
366+
367+ None
368+ }
369+
370+ fn check_hypot ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , args : & [ Expr < ' _ > ] ) {
371+ if let Some ( message) = detect_hypot ( cx, args) {
372+ span_lint_and_sugg (
373+ cx,
374+ IMPRECISE_FLOPS ,
375+ expr. span ,
376+ "hypotenuse can be computed more accurately" ,
377+ "consider using" ,
378+ message,
379+ Applicability :: MachineApplicable ,
380+ ) ;
381+ }
382+ }
383+
322384// TODO: Lint expressions of the form `x.exp() - y` where y > 1
323385// and suggest usage of `x.exp_m1() - (y - 1)` instead
324386fn check_expm1 ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) {
@@ -370,6 +432,14 @@ fn check_mul_add(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
370432 rhs,
371433 ) = & expr. kind
372434 {
435+ if let Some ( parent) = get_parent_expr ( cx, expr) {
436+ if let ExprKind :: MethodCall ( PathSegment { ident : method_name, .. } , _, args) = parent. kind {
437+ if method_name. as_str ( ) == "sqrt" && detect_hypot ( cx, args) . is_some ( ) {
438+ return ;
439+ }
440+ }
441+ }
442+
373443 let ( recv, arg1, arg2) = if let Some ( ( inner_lhs, inner_rhs) ) = is_float_mul_expr ( cx, lhs) {
374444 ( inner_lhs, inner_rhs, rhs)
375445 } else if let Some ( ( inner_lhs, inner_rhs) ) = is_float_mul_expr ( cx, rhs) {
@@ -516,6 +586,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
516586 "log" => check_log_base ( cx, expr, args) ,
517587 "powf" => check_powf ( cx, expr, args) ,
518588 "powi" => check_powi ( cx, expr, args) ,
589+ "sqrt" => check_hypot ( cx, expr, args) ,
519590 _ => { } ,
520591 }
521592 }
0 commit comments