Skip to content

Commit

Permalink
Fix issue #4892.
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-dev-oss committed Apr 17, 2020
1 parent 82be9dc commit 9c89cf0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
24 changes: 23 additions & 1 deletion clippy_lints/src/precedence.rs
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/precedence.fixed
Expand Up @@ -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);
}
16 changes: 16 additions & 0 deletions tests/ui/precedence.rs
Expand Up @@ -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);
}

0 comments on commit 9c89cf0

Please sign in to comment.