Skip to content

Commit 144d1bb

Browse files
committed
Remove unary neg from clippy::precedence lint
1 parent 0ba1e14 commit 144d1bb

File tree

6 files changed

+4
-153
lines changed

6 files changed

+4
-153
lines changed

src/tools/clippy/clippy_lints/src/precedence.rs

+1-52
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
3-
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, MethodCall, UnOp};
4-
use rustc_ast::token;
3+
use rustc_ast::ast::{BinOpKind, Expr, ExprKind};
54
use rustc_errors::Applicability;
65
use rustc_lint::{EarlyContext, EarlyLintPass};
76
use rustc_session::declare_lint_pass;
87
use rustc_span::source_map::Spanned;
98

10-
const ALLOWED_ODD_FUNCTIONS: [&str; 14] = [
11-
"asin",
12-
"asinh",
13-
"atan",
14-
"atanh",
15-
"cbrt",
16-
"fract",
17-
"round",
18-
"signum",
19-
"sin",
20-
"sinh",
21-
"tan",
22-
"tanh",
23-
"to_degrees",
24-
"to_radians",
25-
];
26-
279
declare_clippy_lint! {
2810
/// ### What it does
2911
/// Checks for operations where precedence may be unclear
@@ -41,7 +23,6 @@ declare_clippy_lint! {
4123
///
4224
/// ### Example
4325
/// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
44-
/// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
4526
#[clippy::version = "pre 1.29.0"]
4627
pub PRECEDENCE,
4728
complexity,
@@ -104,38 +85,6 @@ impl EarlyLintPass for Precedence {
10485
(false, false) => (),
10586
}
10687
}
107-
108-
if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
109-
let mut arg = operand;
110-
111-
let mut all_odd = true;
112-
while let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &arg.kind {
113-
let seg_str = seg.ident.name.as_str();
114-
all_odd &= ALLOWED_ODD_FUNCTIONS
115-
.iter()
116-
.any(|odd_function| **odd_function == *seg_str);
117-
arg = receiver;
118-
}
119-
120-
if !all_odd
121-
&& let ExprKind::Lit(lit) = &arg.kind
122-
&& let token::LitKind::Integer | token::LitKind::Float = &lit.kind
123-
{
124-
let mut applicability = Applicability::MachineApplicable;
125-
span_lint_and_sugg(
126-
cx,
127-
PRECEDENCE,
128-
expr.span,
129-
"unary minus has lower precedence than method call",
130-
"consider adding parentheses to clarify your intent",
131-
format!(
132-
"-({})",
133-
snippet_with_applicability(cx, operand.span, "..", &mut applicability)
134-
),
135-
applicability,
136-
);
137-
}
138-
}
13988
}
14089
}
14190

src/tools/clippy/tests/ui/precedence.fixed

-34
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,6 @@ fn main() {
2020
1 ^ (1 - 1);
2121
3 | (2 - 1);
2222
3 & (5 - 2);
23-
-(1i32.abs());
24-
-(1f32.abs());
25-
26-
// These should not trigger an error
27-
let _ = (-1i32).abs();
28-
let _ = (-1f32).abs();
29-
let _ = -(1i32).abs();
30-
let _ = -(1f32).abs();
31-
let _ = -(1i32.abs());
32-
let _ = -(1f32.abs());
33-
34-
// Odd functions should not trigger an error
35-
let _ = -1f64.asin();
36-
let _ = -1f64.asinh();
37-
let _ = -1f64.atan();
38-
let _ = -1f64.atanh();
39-
let _ = -1f64.cbrt();
40-
let _ = -1f64.fract();
41-
let _ = -1f64.round();
42-
let _ = -1f64.signum();
43-
let _ = -1f64.sin();
44-
let _ = -1f64.sinh();
45-
let _ = -1f64.tan();
46-
let _ = -1f64.tanh();
47-
let _ = -1f64.to_degrees();
48-
let _ = -1f64.to_radians();
49-
50-
// Chains containing any non-odd function should trigger (issue #5924)
51-
let _ = -(1.0_f64.cos().cos());
52-
let _ = -(1.0_f64.cos().sin());
53-
let _ = -(1.0_f64.sin().cos());
54-
55-
// Chains of odd functions shouldn't trigger
56-
let _ = -1f64.sin().sin();
5723

5824
let b = 3;
5925
trip!(b * 8);

src/tools/clippy/tests/ui/precedence.rs

-34
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,6 @@ fn main() {
2020
1 ^ 1 - 1;
2121
3 | 2 - 1;
2222
3 & 5 - 2;
23-
-1i32.abs();
24-
-1f32.abs();
25-
26-
// These should not trigger an error
27-
let _ = (-1i32).abs();
28-
let _ = (-1f32).abs();
29-
let _ = -(1i32).abs();
30-
let _ = -(1f32).abs();
31-
let _ = -(1i32.abs());
32-
let _ = -(1f32.abs());
33-
34-
// Odd functions should not trigger an error
35-
let _ = -1f64.asin();
36-
let _ = -1f64.asinh();
37-
let _ = -1f64.atan();
38-
let _ = -1f64.atanh();
39-
let _ = -1f64.cbrt();
40-
let _ = -1f64.fract();
41-
let _ = -1f64.round();
42-
let _ = -1f64.signum();
43-
let _ = -1f64.sin();
44-
let _ = -1f64.sinh();
45-
let _ = -1f64.tan();
46-
let _ = -1f64.tanh();
47-
let _ = -1f64.to_degrees();
48-
let _ = -1f64.to_radians();
49-
50-
// Chains containing any non-odd function should trigger (issue #5924)
51-
let _ = -1.0_f64.cos().cos();
52-
let _ = -1.0_f64.cos().sin();
53-
let _ = -1.0_f64.sin().cos();
54-
55-
// Chains of odd functions shouldn't trigger
56-
let _ = -1f64.sin().sin();
5723

5824
let b = 3;
5925
trip!(b * 8);

src/tools/clippy/tests/ui/precedence.stderr

+1-31
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,5 @@ error: operator precedence can trip the unwary
4343
LL | 3 & 5 - 2;
4444
| ^^^^^^^^^ help: consider parenthesizing your expression: `3 & (5 - 2)`
4545

46-
error: unary minus has lower precedence than method call
47-
--> $DIR/precedence.rs:23:5
48-
|
49-
LL | -1i32.abs();
50-
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1i32.abs())`
51-
52-
error: unary minus has lower precedence than method call
53-
--> $DIR/precedence.rs:24:5
54-
|
55-
LL | -1f32.abs();
56-
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1f32.abs())`
57-
58-
error: unary minus has lower precedence than method call
59-
--> $DIR/precedence.rs:51:13
60-
|
61-
LL | let _ = -1.0_f64.cos().cos();
62-
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.cos().cos())`
63-
64-
error: unary minus has lower precedence than method call
65-
--> $DIR/precedence.rs:52:13
66-
|
67-
LL | let _ = -1.0_f64.cos().sin();
68-
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.cos().sin())`
69-
70-
error: unary minus has lower precedence than method call
71-
--> $DIR/precedence.rs:53:13
72-
|
73-
LL | let _ = -1.0_f64.sin().cos();
74-
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.sin().cos())`
75-
76-
error: aborting due to 12 previous errors
46+
error: aborting due to 7 previous errors
7747

src/tools/clippy/tests/ui/unnecessary_cast.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ mod fixable {
205205

206206
fn issue_9563() {
207207
let _: f64 = (-8.0_f64).exp();
208-
#[allow(clippy::precedence)]
208+
#[allow(ambiguous_unary_precedence)]
209209
let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
210210
}
211211

src/tools/clippy/tests/ui/unnecessary_cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ mod fixable {
205205

206206
fn issue_9563() {
207207
let _: f64 = (-8.0 as f64).exp();
208-
#[allow(clippy::precedence)]
208+
#[allow(ambiguous_unary_precedence)]
209209
let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
210210
}
211211

0 commit comments

Comments
 (0)