@@ -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} ;
@@ -296,6 +296,17 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
296296fn check_powi ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , args : & [ Expr < ' _ > ] ) {
297297 // Check argument
298298 if let Some ( ( value, _) ) = constant ( cx, cx. tables , & args[ 1 ] ) {
299+ // TODO: need more specific check. this is too wide. remember also to include tests
300+ if let Some ( parent) = get_parent_expr ( cx, expr) {
301+ if let Some ( grandparent) = get_parent_expr ( cx, parent) {
302+ if let ExprKind :: MethodCall ( PathSegment { ident : method_name, .. } , _, args) = grandparent. kind {
303+ if method_name. as_str ( ) == "sqrt" && detect_hypot ( cx, args) . is_some ( ) {
304+ return ;
305+ }
306+ }
307+ }
308+ }
309+
299310 let ( lint, help, suggestion) = match value {
300311 Int ( 2 ) => (
301312 IMPRECISE_FLOPS ,
@@ -317,6 +328,57 @@ fn check_powi(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
317328 }
318329}
319330
331+ fn detect_hypot ( cx : & LateContext < ' _ , ' _ > , args : & [ Expr < ' _ > ] ) -> Option < String > {
332+ if let ExprKind :: Binary (
333+ Spanned {
334+ node : BinOpKind :: Add , ..
335+ } ,
336+ ref add_lhs,
337+ ref add_rhs,
338+ ) = args[ 0 ] . kind
339+ {
340+ // check if expression of the form x * x + y * y
341+ if_chain ! {
342+ if let ExprKind :: Binary ( Spanned { node: BinOpKind :: Mul , .. } , ref lmul_lhs, ref lmul_rhs) = add_lhs. kind;
343+ if let ExprKind :: Binary ( Spanned { node: BinOpKind :: Mul , .. } , ref rmul_lhs, ref rmul_rhs) = add_rhs. kind;
344+ if are_exprs_equal( cx, lmul_lhs, lmul_rhs) ;
345+ if are_exprs_equal( cx, rmul_lhs, rmul_rhs) ;
346+ then {
347+ return Some ( format!( "{}.hypot({})" , Sugg :: hir( cx, & lmul_lhs, ".." ) , Sugg :: hir( cx, & rmul_lhs, ".." ) ) ) ;
348+ }
349+ }
350+
351+ // check if expression of the form x.powi(2) + y.powi(2)
352+ if_chain ! {
353+ if let ExprKind :: MethodCall ( PathSegment { ident: lmethod_name, .. } , ref _lspan, ref largs) = add_lhs. kind;
354+ if let ExprKind :: MethodCall ( PathSegment { ident: rmethod_name, .. } , ref _rspan, ref rargs) = add_rhs. kind;
355+ if lmethod_name. as_str( ) == "powi" && rmethod_name. as_str( ) == "powi" ;
356+ if let Some ( ( lvalue, _) ) = constant( cx, cx. tables, & largs[ 1 ] ) ;
357+ if let Some ( ( rvalue, _) ) = constant( cx, cx. tables, & rargs[ 1 ] ) ;
358+ if Int ( 2 ) == lvalue && Int ( 2 ) == rvalue;
359+ then {
360+ return Some ( format!( "{}.hypot({})" , Sugg :: hir( cx, & largs[ 0 ] , ".." ) , Sugg :: hir( cx, & rargs[ 0 ] , ".." ) ) ) ;
361+ }
362+ }
363+ }
364+
365+ None
366+ }
367+
368+ fn check_hypot ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , args : & [ Expr < ' _ > ] ) {
369+ if let Some ( message) = detect_hypot ( cx, args) {
370+ span_lint_and_sugg (
371+ cx,
372+ IMPRECISE_FLOPS ,
373+ expr. span ,
374+ "hypotenuse can be computed more accurately" ,
375+ "consider using" ,
376+ message,
377+ Applicability :: MachineApplicable ,
378+ ) ;
379+ }
380+ }
381+
320382// TODO: Lint expressions of the form `x.exp() - y` where y > 1
321383// and suggest usage of `x.exp_m1() - (y - 1)` instead
322384fn check_expm1 ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
@@ -368,6 +430,14 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
368430 rhs,
369431 ) = & expr. kind
370432 {
433+ if let Some ( parent) = get_parent_expr ( cx, expr) {
434+ if let ExprKind :: MethodCall ( PathSegment { ident : method_name, .. } , _, args) = parent. kind {
435+ if method_name. as_str ( ) == "sqrt" && detect_hypot ( cx, args) . is_some ( ) {
436+ return ;
437+ }
438+ }
439+ }
440+
371441 let ( recv, arg1, arg2) = if let Some ( ( inner_lhs, inner_rhs) ) = is_float_mul_expr ( cx, lhs) {
372442 ( inner_lhs, inner_rhs, rhs)
373443 } else if let Some ( ( inner_lhs, inner_rhs) ) = is_float_mul_expr ( cx, rhs) {
@@ -514,6 +584,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
514584 "log" => check_log_base ( cx, expr, args) ,
515585 "powf" => check_powf ( cx, expr, args) ,
516586 "powi" => check_powi ( cx, expr, args) ,
587+ "sqrt" => check_hypot ( cx, expr, args) ,
517588 _ => { } ,
518589 }
519590 }
0 commit comments