Auto merge of #5928 - mikerite:fix-5924, r=ebroto
Fix false positive in `PRECEDENCE` lint Extend the lint to handle chains of methods combined with unary negation. Closes #5924 changelog: Fix false negative in `PRECEDENCE` lint
This commit is contained in:
commit
4104611913
@ -1,4 +1,5 @@
|
|||||||
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
|
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_ast::ast::{BinOpKind, Expr, ExprKind, LitKind, UnOp};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
@ -102,19 +103,23 @@ impl EarlyLintPass for Precedence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind {
|
if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
|
||||||
if let ExprKind::MethodCall(ref path_segment, ref args, _) = rhs.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();
|
let path_segment_str = path_segment.ident.name.as_str();
|
||||||
if let Some(slf) = args.first() {
|
all_odd &= ALLOWED_ODD_FUNCTIONS
|
||||||
if let ExprKind::Lit(ref lit) = slf.kind {
|
|
||||||
match lit.kind {
|
|
||||||
LitKind::Int(..) | LitKind::Float(..) => {
|
|
||||||
if ALLOWED_ODD_FUNCTIONS
|
|
||||||
.iter()
|
.iter()
|
||||||
.any(|odd_function| **odd_function == *path_segment_str)
|
.any(|odd_function| **odd_function == *path_segment_str);
|
||||||
{
|
arg = args.first().expect("A method always has a receiver.");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
@ -124,14 +129,10 @@ impl EarlyLintPass for Precedence {
|
|||||||
"consider adding parentheses to clarify your intent",
|
"consider adding parentheses to clarify your intent",
|
||||||
format!(
|
format!(
|
||||||
"-({})",
|
"-({})",
|
||||||
snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
|
snippet_with_applicability(cx, operand.span, "..", &mut applicability)
|
||||||
),
|
),
|
||||||
applicability,
|
applicability,
|
||||||
);
|
);
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,14 @@ fn main() {
|
|||||||
let _ = -1f64.to_degrees();
|
let _ = -1f64.to_degrees();
|
||||||
let _ = -1f64.to_radians();
|
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;
|
let b = 3;
|
||||||
trip!(b * 8);
|
trip!(b * 8);
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,14 @@ fn main() {
|
|||||||
let _ = -1f64.to_degrees();
|
let _ = -1f64.to_degrees();
|
||||||
let _ = -1f64.to_radians();
|
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;
|
let b = 3;
|
||||||
trip!(b * 8);
|
trip!(b * 8);
|
||||||
}
|
}
|
||||||
|
@ -54,5 +54,23 @@ error: unary minus has lower precedence than method call
|
|||||||
LL | -1f32.abs();
|
LL | -1f32.abs();
|
||||||
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user