mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
Remove unary neg from clippy::precedence
lint
This commit is contained in:
parent
920cbcdd45
commit
5f3a6e1805
6 changed files with 4 additions and 156 deletions
|
@ -1,38 +1,17 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::source::snippet_with_applicability;
|
use clippy_utils::source::snippet_with_applicability;
|
||||||
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, MethodCall, UnOp};
|
use rustc_ast::ast::{BinOpKind, Expr, ExprKind};
|
||||||
use rustc_ast::token;
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_session::declare_lint_pass;
|
use rustc_session::declare_lint_pass;
|
||||||
use rustc_span::source_map::Spanned;
|
use rustc_span::source_map::Spanned;
|
||||||
|
|
||||||
const ALLOWED_ODD_FUNCTIONS: [&str; 14] = [
|
|
||||||
"asin",
|
|
||||||
"asinh",
|
|
||||||
"atan",
|
|
||||||
"atanh",
|
|
||||||
"cbrt",
|
|
||||||
"fract",
|
|
||||||
"round",
|
|
||||||
"signum",
|
|
||||||
"sin",
|
|
||||||
"sinh",
|
|
||||||
"tan",
|
|
||||||
"tanh",
|
|
||||||
"to_degrees",
|
|
||||||
"to_radians",
|
|
||||||
];
|
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// ### What it does
|
/// ### What it does
|
||||||
/// Checks for operations where precedence may be unclear
|
/// Checks for operations where precedence may be unclear
|
||||||
/// and suggests to add parentheses. Currently it catches the following:
|
/// and suggests to add parentheses. Currently it catches the following:
|
||||||
/// * mixed usage of arithmetic and bit shifting/combining operators without
|
/// * mixed usage of arithmetic and bit shifting/combining operators without
|
||||||
/// parentheses
|
/// parentheses
|
||||||
/// * a "negative" numeric literal (which is really a unary `-` followed by a
|
|
||||||
/// numeric literal)
|
|
||||||
/// followed by a method call
|
|
||||||
///
|
///
|
||||||
/// ### Why is this bad?
|
/// ### Why is this bad?
|
||||||
/// Not everyone knows the precedence of those operators by
|
/// Not everyone knows the precedence of those operators by
|
||||||
|
@ -41,7 +20,6 @@ declare_clippy_lint! {
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
|
/// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
|
||||||
/// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
|
|
||||||
#[clippy::version = "pre 1.29.0"]
|
#[clippy::version = "pre 1.29.0"]
|
||||||
pub PRECEDENCE,
|
pub PRECEDENCE,
|
||||||
complexity,
|
complexity,
|
||||||
|
@ -104,38 +82,6 @@ impl EarlyLintPass for Precedence {
|
||||||
(false, false) => (),
|
(false, false) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
|
|
||||||
let mut arg = operand;
|
|
||||||
|
|
||||||
let mut all_odd = true;
|
|
||||||
while let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &arg.kind {
|
|
||||||
let seg_str = seg.ident.name.as_str();
|
|
||||||
all_odd &= ALLOWED_ODD_FUNCTIONS
|
|
||||||
.iter()
|
|
||||||
.any(|odd_function| **odd_function == *seg_str);
|
|
||||||
arg = receiver;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !all_odd
|
|
||||||
&& let ExprKind::Lit(lit) = &arg.kind
|
|
||||||
&& let token::LitKind::Integer | token::LitKind::Float = &lit.kind
|
|
||||||
{
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,40 +20,6 @@ fn main() {
|
||||||
1 ^ (1 - 1);
|
1 ^ (1 - 1);
|
||||||
3 | (2 - 1);
|
3 | (2 - 1);
|
||||||
3 & (5 - 2);
|
3 & (5 - 2);
|
||||||
-(1i32.abs());
|
|
||||||
-(1f32.abs());
|
|
||||||
|
|
||||||
// These should not trigger an error
|
|
||||||
let _ = (-1i32).abs();
|
|
||||||
let _ = (-1f32).abs();
|
|
||||||
let _ = -(1i32).abs();
|
|
||||||
let _ = -(1f32).abs();
|
|
||||||
let _ = -(1i32.abs());
|
|
||||||
let _ = -(1f32.abs());
|
|
||||||
|
|
||||||
// Odd functions should 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();
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
|
@ -20,40 +20,6 @@ fn main() {
|
||||||
1 ^ 1 - 1;
|
1 ^ 1 - 1;
|
||||||
3 | 2 - 1;
|
3 | 2 - 1;
|
||||||
3 & 5 - 2;
|
3 & 5 - 2;
|
||||||
-1i32.abs();
|
|
||||||
-1f32.abs();
|
|
||||||
|
|
||||||
// These should not trigger an error
|
|
||||||
let _ = (-1i32).abs();
|
|
||||||
let _ = (-1f32).abs();
|
|
||||||
let _ = -(1i32).abs();
|
|
||||||
let _ = -(1f32).abs();
|
|
||||||
let _ = -(1i32.abs());
|
|
||||||
let _ = -(1f32.abs());
|
|
||||||
|
|
||||||
// Odd functions should 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();
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
|
@ -43,35 +43,5 @@ error: operator precedence can trip the unwary
|
||||||
LL | 3 & 5 - 2;
|
LL | 3 & 5 - 2;
|
||||||
| ^^^^^^^^^ help: consider parenthesizing your expression: `3 & (5 - 2)`
|
| ^^^^^^^^^ help: consider parenthesizing your expression: `3 & (5 - 2)`
|
||||||
|
|
||||||
error: unary minus has lower precedence than method call
|
error: aborting due to 7 previous errors
|
||||||
--> tests/ui/precedence.rs:23:5
|
|
||||||
|
|
|
||||||
LL | -1i32.abs();
|
|
||||||
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1i32.abs())`
|
|
||||||
|
|
||||||
error: unary minus has lower precedence than method call
|
|
||||||
--> tests/ui/precedence.rs:24:5
|
|
||||||
|
|
|
||||||
LL | -1f32.abs();
|
|
||||||
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1f32.abs())`
|
|
||||||
|
|
||||||
error: unary minus has lower precedence than method call
|
|
||||||
--> tests/ui/precedence.rs:51: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
|
|
||||||
--> tests/ui/precedence.rs:52: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
|
|
||||||
--> tests/ui/precedence.rs:53: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
|
|
||||||
|
|
||||||
|
|
|
@ -206,7 +206,7 @@ mod fixable {
|
||||||
|
|
||||||
fn issue_9563() {
|
fn issue_9563() {
|
||||||
let _: f64 = (-8.0_f64).exp();
|
let _: f64 = (-8.0_f64).exp();
|
||||||
#[allow(clippy::precedence)]
|
#[allow(ambiguous_negative_literals)]
|
||||||
let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
|
let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -206,7 +206,7 @@ mod fixable {
|
||||||
|
|
||||||
fn issue_9563() {
|
fn issue_9563() {
|
||||||
let _: f64 = (-8.0 as f64).exp();
|
let _: f64 = (-8.0 as f64).exp();
|
||||||
#[allow(clippy::precedence)]
|
#[allow(ambiguous_negative_literals)]
|
||||||
let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
|
let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue