From 9c89cf00c07daa35bb03909708d9597189e04def Mon Sep 17 00:00:00 2001 From: logan-dev-oss Date: Fri, 10 Apr 2020 10:40:49 +0200 Subject: [PATCH] Fix issue #4892. --- clippy_lints/src/precedence.rs | 24 +++++++++++++++++++++++- tests/ui/precedence.fixed | 16 ++++++++++++++++ tests/ui/precedence.rs | 16 ++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/precedence.rs b/clippy_lints/src/precedence.rs index 8b6d0336b20..61c3cc37ae0 100644 --- a/clippy_lints/src/precedence.rs +++ b/clippy_lints/src/precedence.rs @@ -5,6 +5,23 @@ use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; +const ODD_FUNCTIONS_WHITELIST: [&str; 14] = [ + "asin", + "asinh", + "atan", + "atanh", + "cbrt", + "fract", + "round", + "signum", + "sin", + "sinh", + "tan", + "tanh", + "to_degrees", + "to_radians", +]; + declare_clippy_lint! { /// **What it does:** Checks for operations where precedence may be unclear /// and suggests to add parentheses. Currently it catches the following: @@ -86,11 +103,16 @@ impl EarlyLintPass for Precedence { } if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind { - if let ExprKind::MethodCall(_, ref args) = rhs.kind { + if let ExprKind::MethodCall(ref path_segment, ref args) = rhs.kind { if let Some(slf) = args.first() { if let ExprKind::Lit(ref lit) = slf.kind { match lit.kind { LitKind::Int(..) | LitKind::Float(..) => { + for &odd_function in &ODD_FUNCTIONS_WHITELIST { + if odd_function == &*path_segment.ident.name.as_str() { + return; + } + } let mut applicability = Applicability::MachineApplicable; span_lint_and_sugg( cx, diff --git a/tests/ui/precedence.fixed b/tests/ui/precedence.fixed index 0ec85bc47e7..17b1f1bd0bf 100644 --- a/tests/ui/precedence.fixed +++ b/tests/ui/precedence.fixed @@ -32,6 +32,22 @@ fn main() { let _ = -(1i32.abs()); let _ = -(1f32.abs()); + // Odd functions shoud not trigger an error + let _ = -1f64.asin(); + let _ = -1f64.asinh(); + let _ = -1f64.atan(); + let _ = -1f64.atanh(); + let _ = -1f64.cbrt(); + let _ = -1f64.fract(); + let _ = -1f64.round(); + let _ = -1f64.signum(); + let _ = -1f64.sin(); + let _ = -1f64.sinh(); + let _ = -1f64.tan(); + let _ = -1f64.tanh(); + let _ = -1f64.to_degrees(); + let _ = -1f64.to_radians(); + let b = 3; trip!(b * 8); } diff --git a/tests/ui/precedence.rs b/tests/ui/precedence.rs index 4ef771c314f..2d0891fd3c2 100644 --- a/tests/ui/precedence.rs +++ b/tests/ui/precedence.rs @@ -32,6 +32,22 @@ fn main() { let _ = -(1i32.abs()); let _ = -(1f32.abs()); + // Odd functions shoud not trigger an error + let _ = -1f64.asin(); + let _ = -1f64.asinh(); + let _ = -1f64.atan(); + let _ = -1f64.atanh(); + let _ = -1f64.cbrt(); + let _ = -1f64.fract(); + let _ = -1f64.round(); + let _ = -1f64.signum(); + let _ = -1f64.sin(); + let _ = -1f64.sinh(); + let _ = -1f64.tan(); + let _ = -1f64.tanh(); + let _ = -1f64.to_degrees(); + let _ = -1f64.to_radians(); + let b = 3; trip!(b * 8); }