Skip to content

Commit

Permalink
Auto merge of rust-lang#5928 - mikerite:fix-5924, r=ebroto
Browse files Browse the repository at this point in the history
Fix false positive in `PRECEDENCE` lint

Extend the lint to handle chains of methods combined with unary negation.

Closes rust-lang#5924

changelog: Fix false negative in `PRECEDENCE` lint
  • Loading branch information
bors committed Aug 20, 2020
2 parents d891954 + c236c0f commit 4104611
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
59 changes: 30 additions & 29 deletions clippy_lints/src/precedence.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, LitKind, UnOp};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
Expand Down Expand Up @@ -102,36 +103,36 @@ impl EarlyLintPass for Precedence {
}
}

if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind {
if let ExprKind::MethodCall(ref path_segment, ref args, _) = rhs.kind {
if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
let mut arg = operand;

let mut all_odd = true;
while let ExprKind::MethodCall(path_segment, args, _) = &arg.kind {
let path_segment_str = path_segment.ident.name.as_str();
if let Some(slf) = args.first() {
if let ExprKind::Lit(ref lit) = slf.kind {
match lit.kind {
LitKind::Int(..) | LitKind::Float(..) => {
if ALLOWED_ODD_FUNCTIONS
.iter()
.any(|odd_function| **odd_function == *path_segment_str)
{
return;
}
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
PRECEDENCE,
expr.span,
"unary minus has lower precedence than method call",
"consider adding parentheses to clarify your intent",
format!(
"-({})",
snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
),
applicability,
);
},
_ => (),
}
}
all_odd &= ALLOWED_ODD_FUNCTIONS
.iter()
.any(|odd_function| **odd_function == *path_segment_str);
arg = args.first().expect("A method always has a receiver.");
}

if_chain! {
if !all_odd;
if let ExprKind::Lit(lit) = &arg.kind;
if let LitKind::Int(..) | LitKind::Float(..) = &lit.kind;
then {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
PRECEDENCE,
expr.span,
"unary minus has lower precedence than method call",
"consider adding parentheses to clarify your intent",
format!(
"-({})",
snippet_with_applicability(cx, operand.span, "..", &mut applicability)
),
applicability,
);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/precedence.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ fn main() {
let _ = -1f64.to_degrees();
let _ = -1f64.to_radians();

// Chains containing any non-odd function should trigger (issue #5924)
let _ = -(1.0_f64.cos().cos());
let _ = -(1.0_f64.cos().sin());
let _ = -(1.0_f64.sin().cos());

// Chains of odd functions shouldn't trigger
let _ = -1f64.sin().sin();

let b = 3;
trip!(b * 8);
}
8 changes: 8 additions & 0 deletions tests/ui/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ fn main() {
let _ = -1f64.to_degrees();
let _ = -1f64.to_radians();

// Chains containing any non-odd function should trigger (issue #5924)
let _ = -1.0_f64.cos().cos();
let _ = -1.0_f64.cos().sin();
let _ = -1.0_f64.sin().cos();

// Chains of odd functions shouldn't trigger
let _ = -1f64.sin().sin();

let b = 3;
trip!(b * 8);
}
20 changes: 19 additions & 1 deletion tests/ui/precedence.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,23 @@ error: unary minus has lower precedence than method call
LL | -1f32.abs();
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1f32.abs())`

error: aborting due to 9 previous errors
error: unary minus has lower precedence than method call
--> $DIR/precedence.rs:52:13
|
LL | let _ = -1.0_f64.cos().cos();
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.cos().cos())`

error: unary minus has lower precedence than method call
--> $DIR/precedence.rs:53:13
|
LL | let _ = -1.0_f64.cos().sin();
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.cos().sin())`

error: unary minus has lower precedence than method call
--> $DIR/precedence.rs:54:13
|
LL | let _ = -1.0_f64.sin().cos();
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.sin().cos())`

error: aborting due to 12 previous errors

0 comments on commit 4104611

Please sign in to comment.